How to Resize and Compress an Image Using C#

Resizing and compressing an image are common tasks in many applications that deal with graphics, such as photo editors, games, or web development. In this blog post, we will learn how to resize and compress an image using C#, a popular programming language for Windows and .NET platforms.

There are different ways to resize and compress an image using C#, but one of the simplest and most efficient methods is to use the System.Drawing namespace, which provides classes and methods for working with images, graphics, and colors. The System.Drawing namespace is part of the System.Drawing.Common NuGet package, which you can install from the Visual Studio Package Manager or by running the following command in the Package Manager Console:

Install-Package System.Drawing.Common

To resize and compress an image using C#, we need to do the following steps:

  1. Load the source image into an Image object, which represents a bitmap or a metafile.
  2. Create a new Bitmap object with the desired width and height, which represents a pixel-based image.
  3. Create a Graphics object from the new bitmap, which provides methods for drawing on the bitmap.
  4. Set the quality and interpolation mode of the graphics object, which affect how the pixels are blended and resized.
  5. Draw the source image on the new bitmap using the graphics object, specifying the destination rectangle and the source rectangle.
  6. Save the new bitmap to a file or a stream, specifying the image format and the quality.

Here is an example of a C# method that resizes and compresses an image and returns the new bitmap:

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

public static Bitmap ResizeAndCompressImage(Image image, int width, int height, int quality)
{
    // Create a new bitmap with the desired size
    Bitmap result = new Bitmap(width, height);

    // Set the resolution of the new bitmap to match the source image
    result.SetResolution(image.HorizontalResolution, image.VerticalResolution);

    // Create a graphics object from the new bitmap
    using (Graphics graphics = Graphics.FromImage(result))
    {
        // Set the quality and interpolation mode of the graphics object
        graphics.CompositingQuality = CompositingQuality.HighQuality;
        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
        graphics.SmoothingMode = SmoothingMode.HighQuality;

        // Draw the source image on the new bitmap
        graphics.DrawImage(image, new Rectangle(0, 0, width, height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
    }

    // Save the new bitmap to a memory stream
    using (MemoryStream stream = new MemoryStream())
    {
        // Get the image codec for the JPEG format
        ImageCodecInfo codec = ImageCodecInfo.GetImageDecoders().FirstOrDefault(c => c.FormatID == ImageFormat.Jpeg.Guid);

        // Create an encoder parameter for the image quality
        EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, quality);

        // Create an encoder parameters object with the quality parameter
        EncoderParameters encoderParams = new EncoderParameters(1);
        encoderParams.Param[0] = qualityParam;

        // Save the new bitmap to the stream with the JPEG codec and the quality parameter
        result.Save(stream, codec, encoderParams);

        // Load the new bitmap from the stream
        return new Bitmap(stream);
    }
}

To use this method, we need to pass the source image, which can be loaded from a file or a stream, and the desired width, height, and quality of the new image. For example, if we want to resize and compress an image named “cat.jpg” to 300×200 pixels and 50% quality, we can do the following:

using System.IO;

// Load the source image from a file
Image image = Image.FromFile("cat.jpg");

// Resize and compress the image to 300x200 pixels and 50% quality
Bitmap resized = ResizeAndCompressImage(image, 300, 200, 50);

// Save the resized and compressed image to a file
resized.Save("cat_resized.jpg", ImageFormat.Jpeg);

// Dispose the image objects to free the resources
image.Dispose();
resized.Dispose();

The result of this code is a new image file named “cat_resized.jpg” with the size of 300×200 pixels and 50% quality. Here is a comparison of the original and the resized and compressed image:

As you can see, the resized and compressed image has a smaller file size and a lower quality than the original image. You can also adjust the quality and the interpolation mode of the graphics object to suit your needs. For example, you can use InterpolationMode.NearestNeighbor for pixel art or CompositingQuality.HighSpeed for faster performance.