Reputation: 7794
I have a Winforms Gui in C# that allows a user to draw a rectangle on a display of a tiff and save the position, height, width etc. Basically, what I want to do is take the saved position, height and width of the rectangle and clip that area into a sep. bitmap that can then be passed to sep. method that will just OCR the new clip of the bitmap only. What is the best way to do this?
Upvotes: 2
Views: 1406
Reputation:
Use Bitmap.Clone() to create a copy of the cropped region.
public Bitmap ClipBitmap(Bitmap src, Rectangle crop)
{
return src.Clone(crop, src.PixelFormat);
}
Upvotes: 0
Reputation: 941465
Use Graphics.DrawImage() to copy the selection portion of the source image. You'll need the overload that takes a source and a destination Rectangle. Create the Graphics instance from Graphics.FromImage() on a new bitmap that has the same size as the rectangle.
public static Bitmap CropImage(Image source, Rectangle crop) {
var bmp = new Bitmap(crop.Width, crop.Height);
using (var gr = Graphics.FromImage(bmp)) {
gr.DrawImage(source, new Rectangle(0, 0, bmp.Width, bmp.Height), crop, GraphicsUnit.Pixel);
}
return bmp;
}
Upvotes: 2