How to resize an Image C#?

Image resizing in C# is a common requirement for optimizing storage space, improving loading times, and adjusting images for different display contexts. The System.Drawing namespace provides the Bitmap class and related classes to handle image manipulation tasks including resizing, compression, and format conversion.

A bitmap consists of pixel data for a graphics image and its attributes. GDI+ supports multiple file formats including BMP, GIF, EXIF, JPG, PNG, and TIFF. You can create images from files, streams, and other sources using Bitmap constructors and save them using the Save method.

Basic Image Resizing

The most straightforward approach to resize an image is to create a new bitmap with the desired dimensions and draw the original image onto it −

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

class Program {
    public static void Main() {
        // Create a sample bitmap (100x100 red square)
        Bitmap originalImage = new Bitmap(100, 100);
        using (Graphics g = Graphics.FromImage(originalImage)) {
            g.Clear(Color.Red);
        }
        
        // Resize to 50x50
        Bitmap resizedImage = ResizeImage(originalImage, 50, 50);
        
        Console.WriteLine($"Original size: {originalImage.Width}x{originalImage.Height}");
        Console.WriteLine($"Resized size: {resizedImage.Width}x{resizedImage.Height}");
        
        originalImage.Dispose();
        resizedImage.Dispose();
    }
    
    public static Bitmap ResizeImage(Image image, int width, int height) {
        Bitmap resizedImage = new Bitmap(width, height);
        using (Graphics graphics = Graphics.FromImage(resizedImage)) {
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics.DrawImage(image, 0, 0, width, height);
        }
        return resizedImage;
    }
}

The output of the above code is −

Original size: 100x100
Resized size: 50x50

Image Compression and Quality Control

When saving resized images, you can control compression quality using EncoderParameters. The following example demonstrates how to compress and save an image with adjustable quality −

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;

class Program {
    public static void Main() {
        // Create a sample bitmap
        Bitmap originalImage = new Bitmap(200, 200);
        using (Graphics g = Graphics.FromImage(originalImage)) {
            g.Clear(Color.Blue);
            g.FillEllipse(Brushes.Yellow, 50, 50, 100, 100);
        }
        
        // Resize and compress
        Bitmap resizedImage = ResizeImage(originalImage, 100, 100);
        string imagePath = CompressAndSaveImage(resizedImage, 75);
        
        Console.WriteLine("Image resized and saved successfully");
        Console.WriteLine($"Saved to: {imagePath}");
        
        originalImage.Dispose();
        resizedImage.Dispose();
    }
    
    public static Bitmap ResizeImage(Image image, int width, int height) {
        Bitmap resizedImage = new Bitmap(width, height);
        using (Graphics graphics = Graphics.FromImage(resizedImage)) {
            graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            graphics.DrawImage(image, 0, 0, width, height);
        }
        return resizedImage;
    }
    
    public static string CompressAndSaveImage(Bitmap inputImage, int quality = 50) {
        string imageSavedPath = "resized_image.jpg";
        try {
            var jpgEncoder = GetEncoder(ImageFormat.Jpeg);
            var imageEncoder = Encoder.Quality;
            var imageEncoderParameters = new EncoderParameters(1);
            var imageEncoderParameter = new EncoderParameter(imageEncoder, quality);
            imageEncoderParameters.Param[0] = imageEncoderParameter;
            
            inputImage.Save(imageSavedPath, jpgEncoder, imageEncoderParameters);
        }
        catch (Exception ex) {
            Console.WriteLine($"Error saving image: {ex.Message}");
            throw;
        }
        return imageSavedPath;
    }
    
    public static ImageCodecInfo GetEncoder(ImageFormat format) {
        ImageCodecInfo imageCodecInfo = null;
        try {
            var codecs = ImageCodecInfo.GetImageEncoders();
            imageCodecInfo = codecs.FirstOrDefault(codec => codec.FormatID == format.Guid);
        }
        catch (Exception ex) {
            Console.WriteLine($"Error getting encoder: {ex.Message}");
            throw;
        }
        return imageCodecInfo;
    }
}

The output of the above code is −

Image resized and saved successfully
Saved to: resized_image.jpg

Maintaining Aspect Ratio

When resizing images, maintaining the aspect ratio prevents distortion. Here's an example that calculates the optimal dimensions −

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

class Program {
    public static void Main() {
        // Create a rectangular sample image (300x200)
        Bitmap originalImage = new Bitmap(300, 200);
        using (Graphics g = Graphics.FromImage(originalImage)) {
            g.Clear(Color.Green);
        }
        
        // Resize maintaining aspect ratio to fit within 150x150
        Bitmap resizedImage = ResizeImageMaintainAspect(originalImage, 150, 150);
        
        Console.WriteLine($"Original: {originalImage.Width}x{originalImage.Height}");
        Console.WriteLine($"Resized: {resizedImage.Width}x{resizedImage.Height}");
        
        originalImage.Dispose();
        resizedImage.Dispose();
    }
    
    public static Bitmap ResizeImageMaintainAspect(Image image, int maxWidth, int maxHeight) {
        double ratioX = (double)maxWidth / image.Width;
        double ratioY = (double)maxHeight / image.Height;
        double ratio = Math.Min(ratioX, ratioY);
        
        int newWidth = (int)(image.Width * ratio);
        int newHeight = (int)(image.Height * ratio);
        
        Bitmap resizedImage = new Bitmap(newWidth, newHeight);
        using (Graphics graphics = Graphics.FromImage(resizedImage)) {
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics.DrawImage(image, 0, 0, newWidth, newHeight);
        }
        return resizedImage;
    }
}

The output of the above code is −

Original: 300x200
Resized: 150x100

Conclusion

Image resizing in C# is accomplished using the Bitmap class and Graphics.DrawImage() method. Key considerations include setting appropriate InterpolationMode for quality, controlling compression with EncoderParameters, and maintaining aspect ratios to prevent image distortion.

Updated on: 2026-03-17T07:04:36+05:30

665 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements