I use the function below to capture an image in a different form. The captured image is then submitted to the OCR application, in this case tesseract. The problem I am facing is that when the image is copied to a bitmap, the quality drops to 96 dpi, and I want to save it from 1 to 1, and at least 300 dpi, and also scale it * 2, because the text in the image is a little small. I did not write a capture function, and I am wondering if anyone has any recommendations on how I can change it to improve the quality of the returned bitmap.
Since then, I learned that the default dpi for capturing images is actually 96dpi, you can increase any text to 120dpi, but it really did not help in this case. The only option is to capture the image and then resize it. So far I have found several ways to do this, one of which I changed to use stretchBlt, and the other, which I create an even larger bitmap, and then split it into this new enlarged bitmap, which has a higher dpi with such same bicubic scaling, high. So far, I can get the correct OCR value of about 75 - 90%, which is not bad, but I did not receive a cookie.
public static Bitmap Capture(IntPtr hwnd, int x, int y, int width, int height)
{
SIZE size;
IntPtr hBitmap;
IntPtr hDC = PlatformInvokeUSER32.GetDC(hwnd);
IntPtr hMemDC = PlatformInvokeGDI32.CreateCompatibleDC(hDC);
size.cx = width;
size.cy = height;
hBitmap = PlatformInvokeGDI32.CreateCompatibleBitmap(hDC, size.cx, size.cy);
if (hBitmap != IntPtr.Zero)
{
IntPtr hOld = (IntPtr)PlatformInvokeGDI32.SelectObject(hMemDC, hBitmap);
bool b = PlatformInvokeGDI32.BitBlt(hMemDC, 0, 0, size.cx, size.cy, hDC, x, y, PlatformInvokeGDI32.SRCOPY);
PlatformInvokeGDI32.SelectObject(hMemDC, hOld);
PlatformInvokeGDI32.DeleteDC(hMemDC);
PlatformInvokeUSER32.ReleaseDC(hwnd, hDC);
Bitmap bmp = System.Drawing.Image.FromHbitmap(hBitmap);
PlatformInvokeGDI32.DeleteObject(hBitmap);
GC.Collect();
return bmp;
}
else
{
return null;
}
}
#endregion
}
Thanks R.