Crop An Image (Bitmap) in C# or VB.NET

I looked all over the net for some sort of function and what I ended up with seemed rather simple function to crop an bitmap.

Here is the function in C#
public Bitmap CropBitmap(Bitmap bitmap, int cropX, int cropY, int cropWidth, int cropHeight)
{
Rectangle rect = new Rectangle(cropX, cropY, cropWidth, cropHeight);
Bitmap cropped = bitmap.Clone(rect, bitmap.PixelFormat);
return cropped;
}

And the same 3-liner in VB.NET
Private Function CropBitmap(ByRef bmp As Bitmap, ByVal cropX As Integer, ByVal cropY As Integer, ByVal cropWidth As Integer, ByVal cropHeight As Integer) As Bitmap
Dim rect As New Rectangle(cropX, cropY, cropWidth, cropHeight)
Dim cropped As Bitmap = bmp.Clone(rect, bmp.PixelFormat)
Return cropped
End Function

(Sorry the format is not that great on my blog here.)

[tags]vb.net, c#, crop, image, bitmap, visual studio[/tags]

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

February 12, 2007 · Dustin · 18 Comments
Posted in: Programming & Internet

  • Jason L

    Thanks for the crop bitmap code. It worked like a charm for my app. I had to modify it a bit though. See I needed a call that would take height and width, but determine the upper-left corner for me.

  • Sudipta Mukherjee

    Works like magic! Thanks a lot!

  • http://www.reelix.za.net/ Reelix

    Works awesomely!

    Thanks alot! This is about the 17th Page I’ve looked through before finding a good one :)

  • farz

    I did like you descrip. But i get the error: out of memory. What should i do to solve the problem.

  • http://www.reelix.za.net/ Reelix

    That “out of memory” exception appears when you attempt to crop a piece of the image outside itself.

    Eg: If the image is 800×600 and you go:

    Bitmap b = CropBitmap(myPic, 700, 500, 200, 200);

    That’ll try select from 700×500 to 900×700, which does not actually exist.

    Hope this helps!

    - Reelix

  • Anonymous

    Wow I went to several sites, and your code is the most simple. And it works great

  • http://www.michaelellerbeck.com Michael

    Public Overloads Shared Function CropBitmap(ByVal srcBitmap As Bitmap, _
    ByVal cropX As Integer, ByVal cropY As Integer, ByVal cropWidth As Integer, ByVal cropHeight As Integer) As Bitmap

    ‘ Create the new bitmap and associated graphics object
    Dim bmp As New Bitmap(cropWidth, cropHeight)

    Dim g As Graphics = Graphics.FromImage(bmp)
    ‘ Draw the specified section of the source bitmap to the new one
    g.DrawImage(srcBitmap, New Rectangle(0, 0, cropWidth, cropHeight), cropX, cropY, cropWidth, cropHeight, _
    GraphicsUnit.Pixel)
    ‘ Clean up
    g.Dispose()

    ‘ Return the bitmap
    Return bmp

    End Function ‘Copy

    I think this one is slightly faster. From

  • Te3ahight

    Thanks for the c# too …

  • Ell

    Thank you so much, you saved me alot of time, this is great code and will credit you in my game (that I will hopefully finish) because without you I would have spent another week figuring this out :)

  • http://www.vbknowledgebase.com Pon

    nice article

  • David Andersson

    Thank you both. I ended up using Michael’s addition.

  • Hscoder
  • George

    Thank you for giving the solution

  • Billy

    Thanks for this.  I was trying to resolve an issue with cropping transparent GIF images, where they ended up with a black background. I was using the Graphics namespace, loading images, etc. Your code helped me to fix the issue. Thanks a lot.

    Here’s the code for anyone interested:

            ///
            /// Crops an image based on specified start coordinates/width and height
            ///
            ///
            ///
            ///
            ///
            ///
            ///
            /// Inspiration from: http://www.nerdydork.com/crop-an-image-bitmap-in-c-or-vbnet.html
            public static byte[] Crop(string pOriginalImage, int pCropWidth, int pCropHeight, int pCropStartX, int pCropStartY)
            {
                using (Bitmap original = new Bitmap(pOriginalImage))
                {
                    Rectangle rect = new Rectangle(pCropStartX, pCropStartY, pCropWidth, pCropHeight);
                    using (Bitmap cropped = original.Clone(rect, original.PixelFormat))
                    {
                        using (MemoryStream ms = new MemoryStream())
                        {
                            cropped.Save(ms, original.RawFormat);
                            return ms.GetBuffer();
                        }
                    }
                }

  • Svmbabu547

    HI, i want crop image functionality after retrieving image into database.please send to my m@ail svmbabu547@gmail.com

  • Andy

    Hi, nice piece of bitmap code. I’d like to “convert” this into a dll. Any idea how? Tried compiling and adding the dll as a Reference to a Windows Form but CropBitmap method refuse to appear..

  • http://imageresizing.net/ Nathanael Jones

    Ever seen http://imageresizing.net ? It brings cropping and resizing to ASP.NET with a URL API, proper memory management, and very optimized routines.

  • Sue

    Genuis!! I’ve been fiddling with examples on codeproject for the best part of a day without achieving what I wanted.  It took 2 minutes to substitute with your code and it works perfectly.
    Thanks.