MartinDrozdik.Services.ImageSaving
2.0.0
dotnet add package MartinDrozdik.Services.ImageSaving --version 2.0.0
NuGet\Install-Package MartinDrozdik.Services.ImageSaving -Version 2.0.0
<PackageReference Include="MartinDrozdik.Services.ImageSaving" Version="2.0.0" />
paket add MartinDrozdik.Services.ImageSaving --version 2.0.0
#r "nuget: MartinDrozdik.Services.ImageSaving, 2.0.0"
// Install MartinDrozdik.Services.ImageSaving as a Cake Addin #addin nuget:?package=MartinDrozdik.Services.ImageSaving&version=2.0.0 // Install MartinDrozdik.Services.ImageSaving as a Cake Tool #tool nuget:?package=MartinDrozdik.Services.ImageSaving&version=2.0.0
Image saving service for quick use
A service to save your images to disk with little effort. It solves the problem with resizing and quality handling.
(!!!) The code uses ImageSharp – check your license and stuff.
Setup
Add the saver to your service collection or straight up instantiate it like a bad boy.
builder.Services.AddImageSaver();
var saver = new ImageSaver();
Usage
Use the following interface:
public interface IImageSaver
{
Task SaveAsync(string path, Stream imageData, CancellationToken cancellationToken);
Task SaveAsync(string path, Stream imageData, IImageConfiguration config, CancellationToken cancellationToken);
Task SaveAsync(Stream imageData, IEnumerable<ImageTarget> targets, CancellationToken cancellationToken);
}
It will save a stream to a path like /you/have/ligma.png
.
The awesome part is the IImageConfiguration
. Checkout what properties this baby can fit in:
public interface IImageConfiguration
{
int Height { get; }
int Width { get; }
int MaxHeight { get; }
int MaxWidth { get; }
int Quality { get; }
}
The saver will try to match your set width/height properties. If you set only one dimension (width/height), the aspect ratio will be preserved!
The quality should be in the range 1-100 and is applied for formats that support it (jpeg, webm).
var saver = new ImageSaver();
await imageSaver.Save(imagePath, image, new ImageConfiguration()
{
MaxWidth = 50,
Quality = 80
});
If all config values are default or the file is of type .svg, the stream is straight up dumped into the file without any modifications.
Under the hood
It uses ImageSharp for all image operations, and that's it.
Checkout the project with tests.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
-
net9.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.1)
- SixLabors.ImageSharp (>= 3.1.6)
- SixLabors.ImageSharp.Drawing (>= 2.1.5)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.