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?
+1
A:
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;
}
Hans Passant
2010-03-08 22:40:51
Also make sure that you're managing your crop rectangle in relative percentages versus absolutes in the event that you're scaling your display of the original image.
Jacob G
2010-03-08 22:47:40
A:
Use Bitmap.Clone() to create a copy of the cropped region.
public Bitmap ClipBitmap(Bitmap src, Rectangle crop)
{
return src.Clone(crop, src.PixelFormat);
}
Tarsier
2010-03-17 12:09:32