Coder Perfect

What is the best way to crop an image in C#?


In C#, how can I develop an application that crops images?

Asked by sandy101

Solution #1

Visit for more information.

private static Image cropImage(Image img, Rectangle cropArea)
   Bitmap bmpImage = new Bitmap(img);
   return bmpImage.Clone(cropArea, bmpImage.PixelFormat);

Answered by Nick

Solution #2

Graphics can be used. DrawImage draws a clipped picture from a bitmap onto the graphics object.

Rectangle cropRect = new Rectangle(...);
Bitmap src = Image.FromFile(fileName) as Bitmap;
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), 

Answered by Daniel LeCheminant

Solution #3

This is a more straightforward solution than the conventional one:

public static Bitmap cropAtRect(this Bitmap b, Rectangle r)
    Bitmap nb = new Bitmap(r.Width, r.Height);
    using (Graphics g = Graphics.FromImage(nb))
        g.DrawImage(b, -r.X, -r.Y);
        return nb;

It also avoids the chance of the simplest answer’s “Out of memory” exception.

Because Bitmap and Graphics are IDisposable, the using clauses are required.

EDIT: I’ve found that this works perfectly with Bitmap-saved PNGs. PNGs saved by Paint Shop Pro 6 or Save or Paint.exe work OK, however the content is misplaced. GraphicsUnit has been added. Pixel produces an incorrect result. Maybe it’s only the PNGs that aren’t working.

Answered by ChrisJJ

Solution #4

Even if you use the best answer above, this may be essential, especially if your image is really good and the resolutions aren’t exactly 96.0.

My test example:

    static Bitmap LoadImage()
        return (Bitmap)Bitmap.FromFile( @"e:\Tests\d_bigImage.bmp" ); // here is large image 9222x9222 pixels and 95.96 dpi resolutions

    static void TestBigImagePartDrawing()
        using( var absentRectangleImage = LoadImage() )
            using( var currentTile = new Bitmap( 256, 256 ) )
                currentTile.SetResolution(absentRectangleImage.HorizontalResolution, absentRectangleImage.VerticalResolution);

                using( var currentTileGraphics = Graphics.FromImage( currentTile ) )
                    currentTileGraphics.Clear( Color.Black );
                    var absentRectangleArea = new Rectangle( 3, 8963, 256, 256 );
                    currentTileGraphics.DrawImage( absentRectangleImage, 0, 0, absentRectangleArea, GraphicsUnit.Pixel );


Answered by IntellyDev

Solution #5

Here’s a quick example of how to crop an image.

public Image Crop(string img, int width, int height, int x, int y)
        Image image = Image.FromFile(img);
        Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
        bmp.SetResolution(80, 60);

        Graphics gfx = Graphics.FromImage(bmp);
        gfx.SmoothingMode = SmoothingMode.AntiAlias;
        gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
        gfx.PixelOffsetMode = PixelOffsetMode.HighQuality;
        gfx.DrawImage(image, new Rectangle(0, 0, width, height), x, y, width, height, GraphicsUnit.Pixel);
        // Dispose to free up resources

        return bmp;
    catch (Exception ex)
        return null;

Answered by PsychoCoder

Post is based on