Language: EN

imagesharp

How to work with images in C# with ImageSharp

ImageSharp is a popular open-source library for image processing in .NET, which has become almost a standard for image processing in C# applications.

It includes support for most popular image formats, and provides advanced manipulation and editing functionalities, as well as format conversion between images.

Some of its features include:

  • Basic image manipulation: Resizing, rotating, cropping, etc.
  • Advanced image processing: Shading, color filtering, tone change, etc.
  • Support for a wide variety of image formats, including JPEG, PNG, BMP, GIF, WebP, SVG, and more.
  • Compatibility with .NET Standard 2.0, .NET Core, and .NET Framework.

How to use ImageSharp

We can easily add XamlFlair to our project via a NuGet package by doing,

Install-Package SixLabors.ImageSharp

To load a WebP image in our C# code we call the Load method of the Image class with the image path to load.

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;

// Load image from file
using (var image = Image.Load("image_path.webp"))
{
    // Change the size of the image
    image.Mutate(x => x.Resize(new Size(800, 600)));

    // Save the modified image
    image.Save("modified_image_path.webp");
}

Next, we can apply transformations to the image using the ‘Mutate’ method, and passing the transformations to be performed.

There are a ton of transformations we can apply including, from the most basic like resize, crop, rotate, contrast, brightness, to more advanced like color adjustment, or edge detection.

Then, we can save the image in any of the available formats using the ‘Save’ method.

Optionally, we can change the parameters of the encoder. For example, to change the quality of a wepb file we would do

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;

// Load image from file
using (var image = Image.Load("image_path.webp"))
{
    // Change the size of the image
    image.Mutate(x => x.Resize(new Size(800, 600)));

    var encoder = new WebpEncoder()
    {
        Quality = 70
    };

    // Save the modified image
    image.Save("modified_image_path.webp", encoder);
}

Converting to drawing image

If we want to use ImageSharp images in a C# application, for example in a WinForms or WPF application, we will need to convert it to a Drawings.Common format.

With these two extension methods

public static byte[] ToArray(this SixLabors.ImageSharp.Image imageIn)
{
    using (MemoryStream ms = new MemoryStream())
    {
        imageIn.Save(ms, PngFormat.Instance);
        return ms.ToArray();
    }
}

public static System.Drawing.Image ToDrawingImage(this byte[] byteArrayIn)
{
    using (MemoryStream ms = new MemoryStream(byteArrayIn))
    {
        System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
        return returnImage;
    }
}

With this we can convert our ImageSharp image like this

var drawingImage = image.ToArray().ToDrawingImage();

If we need a Drawing.Bitmap, we can do the following

var drawingImage = image.ToArray().ToDrawingImage();
var bitmap = new System.Drawing.Bitmap(drawingImage);

With this we could already put the image, for example, in a PictureBox or in any other standard Windows control that displays images.

In this example, we are creating a new PictureBox control and assigning the loaded Image object to the control. Additionally, we are setting the SizeMode property of the control to StretchImage, which automatically adjusts the image to the size of the control.

In short, ImageSharp is a very powerful library for image processing and, except for very special cases, it is almost the only library you will need to work with images in .NET.

In fact, this library is recommended by Microsoft itself as one of the alternatives for working with images, compared to the “mess” they have with their own libraries.

Of course, there are many more options and functions available. If you are interested, the project’s website provides documentation and usage examples.

ImageSharp is Open Source and all the code is available on the project’s website GitHub - SixLabors/ImageSharp: A modern, cross-platform, 2D Graphics library for .NET