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                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MartinDrozdik.Services.ImageSaving" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MartinDrozdik.Services.ImageSaving --version 2.0.0                
#r "nuget: MartinDrozdik.Services.ImageSaving, 2.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 68 2/1/2025
1.3.2 432 9/3/2022
1.3.1 395 9/3/2022
1.3.0 394 9/3/2022
1.2.0 405 8/3/2022
1.1.3 423 7/7/2022
1.1.2 418 7/7/2022
1.1.1 429 4/29/2022
1.1.0 429 4/29/2022
1.0.1 401 4/29/2022
1.0.0 407 4/29/2022