Feb 12 2007

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

Tag: Programming & InternetDustin @ 2:32 pm

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.)

Technorati Tags: vb.net, , , , ,




Clicky Web Analytics