How do I crop an image using C#?

c#, image-processing

Solution

You can use [`Graphics.DrawImage`][1] to draw a cropped image onto the graphics object from a bitmap.

Rectangle cropRect = new Rectangle(...);
using (Bitmap src = Image.FromFile("") as Bitmap)
{
    using (Bitmap target = new Bitmap(cropRect.Width, cropRect.Height))
    {
        using (Graphics g = Graphics.FromImage(target))
        {
            g.DrawImage(src, new Rectangle(0, 0, target.Width, target.Height),
                cropRect,
                GraphicsUnit.Pixel);
        }
    }
}

Problem

How do I crop an image using C#?

Original source