C# - Crop and resize images

By , 12/28/2012
(1 ratings)
This class offers 3 methods to crop and/or resize images.

Author: Chris_Cluss, translated by Jan Welker to C#, translation to English by Michael List
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

public class ImageFunctions
{
    /// <summary>
    /// Crops and resizes the image.
    /// </summary>
    /// <param name="img">The image to be processed</param>
    /// <param name="targetWidth">Width of the target</param>
    /// <param name="targetHeight">Height of the target</param>
    /// <param name="x1">The position x1</param>
    /// <param name="y1">The position y1</param>
    /// <param name="x2">The position x2</param>
    /// <param name="y2">The position y2</param>
    /// <param name="imageFormat">The image format</param>
    /// <returns>A cropped and resized image</returns>
    public Image CropAndResizeImage(Image img, int targetWidth, int targetHeight, int x1, int y1, int x2, int y2, ImageFormat imageFormat)
    {
        var bmp = new Bitmap(targetWidth, targetHeight);
        Graphics g = Graphics.FromImage(bmp);

        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.SmoothingMode = SmoothingMode.HighQuality;
        g.PixelOffsetMode = PixelOffsetMode.HighQuality;
        g.CompositingQuality = CompositingQuality.HighQuality;

        int width = x2 - x1;
        int height = y2 - y1;

        g.DrawImage(img, new Rectangle(0, 0, targetWidth, targetHeight), x1, y1, width, height, GraphicsUnit.Pixel);

        var memStream = new MemoryStream();
        bmp.Save(memStream, imageFormat);
        return Image.FromStream(memStream);
    }

    /// <summary>
    /// Resizes the image.
    /// </summary>
    /// <param name="img">The image to be resized</param>
    /// <param name="targetWidth">Width of the target</param>
    /// <param name="targetHeight">Height of the target</param>
    /// <param name="imageFormat">The image format</param>
    /// <returns>A resized image</returns>
    public Image ResizeImage(Image img, int targetWidth, int targetHeight, System.Drawing.Imaging.ImageFormat imageFormat)
    {
        return CropAndResizeImage(img, targetWidth, targetHeight, 0, 0, img.Width, img.Height, imageFormat);
    }

    /// <summary>
    /// Crops the image.
    /// </summary>
    /// <param name="img">The image</param>
    /// <param name="x1">The position x1.</param>
    /// <param name="y1">The position y1.</param>
    /// <param name="x2">The position x2.</param>
    /// <param name="y2">The position y2.</param>
    /// <param name="imageFormat">The image format.</param>
    /// <returns>A cropped image.</returns>
    public Image CropImage(Image img, int x1, int y1, int x2, int y2, System.Drawing.Imaging.ImageFormat imageFormat)
    {
        return CropAndResizeImage(img, x2 - x1, y2 - y1, x1, y1, x2, y2, imageFormat);
    }
}
Tagged with Image, Bitmap, picture, downsize, crop, resize.

Comments

 

Log in, to comment!