imagesharp

How to work with images in C# with ImageSharp

  • 3 min

ImageSharp is a popular open-source library for image processing in .NET, which has become almost a standard for image handling 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 are:

  • Basic image manipulation: Resizing, rotating, cropping, etc.
  • Advanced image processing: Shading, color filtering, hue shifting, 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 path of the image 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");
}
Copied!

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

There are a lot of transformations we can apply, from basic ones like resize, crop, rotate, contrast, brightness, to more advanced ones 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 encoder parameters. For example, to change the quality of a webp 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);
}
Copied!

Converting to drawing image

If we want to use ImageSharp images in a C# application, for example in a WinForms or WPF application, we must convert them 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;
    }
}
Copied!

With this, we can convert our ImageSharp image like this:

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

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

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

With this, we could now place the image, for example, in a PictureBox or 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 control’s SizeMode property to StretchImage, which makes the image automatically adjust to the size of the control.

In short, ImageSharp is a very powerful library for image processing and, except for very special cases, it’s almost the only library you’ll 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.