Working with Images in C# (.NET): Library Overview and Practical Examples
Image processing is a common task in modern C# (.NET) applications. Whether it's uploading a user avatar, creating thumbnails, adding watermarks, or converting formats — .NET offers several powerful solutions. In this article, we will review the main libraries, their capabilities, and write real code examples.
1. Standard Library: System.Drawing.Common
Historically the first and most well-known is System.Drawing.Common. It provides access to GDI+ (Graphics Device Interface) on Windows. However, it has important limitations:
- Works only on Windows (for .NET Core/.NET 5+ the
System.Drawing.Commonpackage is required, but support on Linux/macOS is limited). - Not optimized for high-load web services (memory leaks if used incorrectly).
- Does not support modern formats (WebP, AVIF) out of the box.
Example of loading and resizing an image:
using System.Drawing;using System.Drawing.Imaging;
public static void ResizeImage(string sourcePath, string destPath, int newWidth, int newHeight){ using (var image = Image.FromFile(sourcePath)) using (var resized = new Bitmap(newWidth, newHeight)) using (var graphics = Graphics.FromImage(resized)) { graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; graphics.DrawImage(image, 0, 0, newWidth, newHeight); resized.Save(destPath, ImageFormat.Jpeg); }}This code loads the source image, creates a new Bitmap with the specified dimensions, and draws the source onto it with high quality. Suitable for simple tasks.
2. Modern Cross-Platform Library: ImageSharp (SixLabors)
ImageSharp is a fully managed library for .NET that does not depend on GDI+. It works on Windows, Linux, and macOS. Key advantages:
- Pure C# code, no native dependencies.
- Support for WebP, PNG, JPEG, BMP, GIF, TIFF.
- Built-in filters, rotations, cropping, resizing.
- High performance and security for web applications.
Installation: dotnet add package SixLabors.ImageSharp
Example of resizing and saving to WebP:
using SixLabors.ImageSharp;using SixLabors.ImageSharp.Processing;using SixLabors.ImageSharp.Formats.Webp;
public static void ResizeToWebp(string input, string output, int width, int height){ using (var image = Image.Load(input)) { image.Mutate(x => x.Resize(width, height)); var encoder = new WebpEncoder { Quality = 80 }; image.Save(output, encoder); }}Note: the Mutate method applies changes to the image itself (without creating a copy), saving memory.
3. Graphics Engine: SkiaSharp
SkiaSharp is a wrapper around the Skia library (used in Chrome, Android, Flutter). It provides maximum flexibility and performance, especially for complex 2D graphics.
- Cross-platform (Windows, Linux, macOS, mobile platforms).
- Hardware acceleration support (GPU).
- Rich set of tools: paths, brushes, shaders, masks.
Example of adding a watermark:
using SkiaSharp;
public static void AddWatermark(string imagePath, string watermarkText, string outputPath){ using (var input = SKBitmap.Decode(imagePath)) using (var canvas = new SKCanvas(input)) using (var paint = new SKPaint { Color = SKColors.White.WithAlpha(128), TextSize =