ImageSharp LogoImageSharp

PM > Install-Package SixLabors.ImageSharp

A Better API - ImageSharp vs System.Drawing

Without the constraints of System.Drawing We have been able to develop something much more flexible, easier to code against, and much, much less prone to memory leaks.

Gone are system-wide process-locks; ImageSharp images are thread-safe and fully supported in web environments.

Here's an example of the code required to load an image from a file, resize it, and save it back to the filesystem as a jpeg with a quality of 75.

System.Drawing

using (Image image = Image.FromFile("foo.jpg", true))
using (Bitmap resized = new Bitmap(image.Width / 2, image.Width / 2))
using (Graphics graphics = Graphics.FromImage(resized))
using (ImageAttributes attributes = new ImageAttributes())
{
    attributes.SetWrapMode(WrapMode.TileFlipXY);
    graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
    graphics.CompositingQuality = CompositingQuality.HighSpeed;
    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
    graphics.CompositingMode = CompositingMode.SourceCopy;
    graphics.DrawImage(image, Rectangle.FromLTRB(0, 0, image.Width / 2, image.Width / 2), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes);

    using (EncoderParameters encoderParameters = new EncoderParameter(Encoder.Quality, 75))
    {
        ImageCodecInfo codec = ImageCodecInfo.GetImageEncoders()
                                             .FirstOrDefault(ici => ici=> ici.FormatID == ImageFormat.Jpeg.Guid));

        using (FileStream output = File.OpenWrite("bar.jpg"))
        {
            resized.Save(output, codec, encoderParameters);
        }
    }
}

ImageSharp .Net Standard 1.3+

.NET Standard 1.3 is the lowest version that supports the filesystem API. We can load/save from streams though from .NET Standard 1.1+.

using (var image = Image.Load("foo.jpg"))
{
    image.Mutate(ctx=>ctx.Resize(image.Width / 2, image.Height / 2));
    image.Save("bar.jpg"); // Automatic encoder selected based on extension.
}

For advanced usage a there are several IPixel struct implementations allowing developers to implement their own color models in the same manner as Microsoft XNA Game Studio and MonoGame.