How to get focused/targeted image?

After i added functionality to draw target/rectangle on preview, i received some emails with question how to grab part that exactly in rectagnle.

Ok you dont need anything special and any lib, for example you draw target with

Rect _rect

then to grab that part you need:

1. Grab full image

Bitmap bmp = new Bitmap(width, height);
System.Drawing.Imaging.BitmapData data = bmp.LockBits(new Rectangle(0, 0, width, height),
System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format16bppRgb565);

bFlag = cam_.getRgb565(data.Scan0);
bmp.UnlockBits(data);

2. Create 2nd bitmap

Bitmap bmp2 = new Bitmap(_rect.Right - _rect.Left, _rect.Bottom - _rect.Top);

3. Using Graphics cut required part

gr.DrawImage(bmp, new Rectangle(0, 0, bmp2.Width, bmp2.Height),
new Rectangle(_rect.Left, _rect.Top, bmp2.Width, bmp2.Height), GraphicsUnit.Pixel);

bmp2 - result bitmap and its exactly what you need

Leave a Reply