Resize Images with .NET Core

Resize Images with .NET Core

To resize images like PNG, JPEG, JPG and Co using .NET Core, there is no SDK support available today.

But the Microsoft Mono team has written a wrapper named SkiaSharp based on the Google library Skia, which works wonderfully and fast - but unfortunately a bit complex. The documentation can also be improved.

Snippet

Here is my snippet to resize images width SkiaSharp

    public class SkiaSharpImageManipulationProvider : IImageManipulationProvider
    {
        public (byte[] FileContents, int Height, int Width) Resize(byte[] fileContents, 
        int maxWidth, int maxHeight, 
        SKFilterQuality quality = SKFilterQuality.Medium)
        {
            using MemoryStream ms = new MemoryStream(fileContents);
            using SKBitmap sourceBitmap = SKBitmap.Decode(ms);

            int height = Math.Min(maxHeight, sourceBitmap.Height);
            int width = Math.Min(maxWidth, sourceBitmap.Width);

            using SKBitmap scaledBitmap = sourceBitmap.Resize(new SKImageInfo(width, height), quality);
            using SKImage scaledImage = SKImage.FromBitmap(scaledBitmap);
            using SKData data = scaledImage.Encode();

            return (data.ToArray(), height, width);
        }
    }