Skip to content

drozdik-m/dotnet-image-saving-service

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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);
    Task SaveAsync(string path, Stream imageData, IImageConfiguration config);
    Task SaveAsync(Stream imageData, params (string path, IImageConfiguration config)[] targets);
}

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; set; }
    int Width { get; set; }
    int MaxHeight { get; set; }
    int MaxWidth { get; set; }
    int Quality { get; set; }
}

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.

About

A service to save your images with little effort

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages