Image Processing in C#: Resize and Crop

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with Images in C#: Resize and Crop



In modern web development and desktop applications, it is often necessary to programmatically process images: reduce them for faster loading, create thumbnails, or crop excess edges. In the .NET ecosystem, there are several approaches to solve these tasks. In this article, we will look at two main ways to resize and crop images in C#: the classic System.Drawing and the modern cross-platform SixLabors.ImageSharp.



1. Using System.Drawing (Windows Forms / .NET Framework)

The classic approach, available in .NET Framework and .NET Core/5+ (with limitations on Linux/macOS). It works through the System.Drawing namespace and the Bitmap class. Suitable for legacy projects or Windows-only applications.



Example: resizing while maintaining aspect ratio

using System.Drawing;using System.Drawing.Drawing2D;using System.Drawing.Imaging;

public static Bitmap ResizeImage(Image image, int newWidth, int newHeight){ var destRect = new Rectangle(0, 0, newWidth, newHeight); var destImage = new Bitmap(newWidth, newHeight);

using (var graphics = Graphics.FromImage(destImage)) { graphics.CompositingMode = CompositingMode.SourceCopy; graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

using (var wrapMode = new ImageAttributes()) { wrapMode.SetWrapMode(WrapMode.TileFlipXY); graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode); } } return destImage;}

// Usage:using (var original = Image.FromFile("input.jpg")){ using (var resized = ResizeImage(original, 800, 600)) { resized.Save("output.jpg", ImageFormat.Jpeg); }}


Example: cropping an image

public static Bitmap CropImage(Image source, Rectangle cropArea){    var bmp = new Bitmap(cropArea.Width, cropArea.Height);    using (var g = Graphics.FromImage(bmp))    {        g.DrawImage(source, new Rectangle(0, 0, bmp.Width, bmp.Height),                    cropArea, GraphicsUnit.Pixel);    }    return bmp;}

// Usage:var cropRect = new Rectangle(100, 50, 300, 200); // X, Y, Width, Heightusing (var cropped = CropImage(Image.FromFile("input.jpg"), cropRect)){ cropped.Save("cropped.jpg", ImageFormat.Jpeg);}


2. SixLabors ImageSharp (Cross-platform .NET)

SixLabors.ImageSharp is a modern, fully managed image processing library that works on .NET Core, .NET 5/6/7+ and .NET Framework. It does not depend on the Windows API, making it ideal for web applications (ASP.NET Core), console utilities, and Docker containers.



Installation via NuGet:

dotnet add package SixLabors.ImageSharp


Example: resizing while maintaining aspect ratio

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

public static void ResizeImageSharp(string inputPath, string outputPath, int width, int height){ using (var image = Image.Load(inputPath)) { // ResizeMode.Pad — fits the image, adding transparent/white padding // ResizeMode.Crop — crops to the specified dimensions // ResizeMode.Max — maintains aspect ratio, fitting into the rectangle image.Mutate(x => x.Resize(width, height, KnownResamplers.Lanczos3)); image.Save(outputPath); }}

// Example with maintaining aspect ratio (au

Recommendations