Working with Images in C# (.NET): Resizing and Cropping
In modern web development and desktop application creation, there is often a need to programmatically process images: resize them for previews, crop excess edges, or adapt them to a specific format. The .NET platform offers several approaches to solving these tasks. In this article, we will thoroughly examine working with the SixLabors.ImageSharp library — a powerful, cross-platform tool for image processing in C#.
Why ImageSharp?
Previously, the standard solution was the System.Drawing library, but it has limitations: it only works on Windows, requires installed GDI+ components, and is not optimized for modern web applications. ImageSharp lacks these drawbacks:
- Fully written in managed C# code.
- Works on Windows, Linux, macOS.
- Supports popular formats: JPEG, PNG, BMP, GIF, WebP.
- High performance and security.
Installing the Library
To begin, create a new C# project (.NET 6+) and install the package via NuGet:
dotnet add package SixLabors.ImageSharpOr via the Visual Studio package manager: Install-Package SixLabors.ImageSharp.
Resizing an Image
The resize function allows you to reduce or enlarge an image, preserving proportions or setting exact dimensions. Let's look at an example where we load an image and reduce it to a width of 300 pixels, automatically adjusting the height:
using SixLabors.ImageSharp;using SixLabors.ImageSharp.Processing;
// Load the source imageusing (Image image = Image.Load("input.jpg")){ // Resize: width 300, height calculated automatically image.Mutate(x => x.Resize(300, 0));
// Save the result image.Save("output_resize.jpg");}By passing 0 as the second parameter, we specify that the height should be calculated automatically to maintain proportions. If you need to set exact dimensions (e.g., 300x200), specify both values:
image.Mutate(x => x.Resize(300, 200)); // May distort proportionsTo preserve proportions with an exact size, use the ResizeMode.Pad or ResizeMode.Crop mode:
image.Mutate(x => x.Resize(new ResizeOptions{ Size = new Size(300, 200), Mode = ResizeMode.Crop // Crops edges to fit the size}));Cropping an Image
Cropping allows you to cut out a rectangular area from the source image. This is useful for creating avatars, thumbnails, or removing empty borders. Example — cropping a central area of 200x150 pixels:
using (Image image = Image.Load("input.jpg")){ // Define the coordinates and size of the area to crop Rectangle cropRect = new Rectangle( x: (image.Width - 200) / 2, // Center horizontally y: (image.Height - 150) / 2, // Center vertically width: 200, height: 150 );
// Perform the crop image.Mutate(x => x.Crop(cropRect));
// Save the result image.Save("output_crop.jpg");}If you need to crop an image manually (e.g., the top-left corner 100x100):
image.Mutate(x => x.Crop(new Rectangle(0, 0, 100, 100)));Combining Operations: Resize + Crop
Often you need to first resize and then crop to the desired format. ImageSharp allows chaining operations together:
using (Image image = Image.Load("input.jpg")){ // Step 1: reduce to a width of 400 pixels (proportionally) image.Mutate(x =>