Capturing images in a different form, how to save dpi with bitmaps

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 contains the size of the screen
            SIZE size;

            //hBitmap contains the handle to the bitmap
            IntPtr hBitmap;

            //Get handle to the desktop device context
            IntPtr hDC = PlatformInvokeUSER32.GetDC(hwnd);

            //Device context in memory for screen device context
            IntPtr hMemDC = PlatformInvokeGDI32.CreateCompatibleDC(hDC);

            //Pass SM_CXSCREEN to GetSystemMetrics to get the X coordinates 
            //of the screen
            size.cx = width;

            //As above but get Y corrdinates of the screen
            size.cy = height;

            //Create a compatiable bitmap of the screen size using the 
            //screen device context
            hBitmap = PlatformInvokeGDI32.CreateCompatibleBitmap(hDC, size.cx, size.cy);

            //Cannot check of IntPtr is null so check against zero instead
            if (hBitmap != IntPtr.Zero)
            {
                //Select the compatiable bitmap in the memeory and keep a 
                //refrence to the old bitmap
                IntPtr hOld = (IntPtr)PlatformInvokeGDI32.SelectObject(hMemDC, hBitmap);

                //Copy bitmap into the memory device context
                bool b = PlatformInvokeGDI32.BitBlt(hMemDC, 0, 0, size.cx, size.cy, hDC, x, y, PlatformInvokeGDI32.SRCOPY);

                //Select the old bitmap back to the memory device context
                PlatformInvokeGDI32.SelectObject(hMemDC, hOld);

                //Delete memory device context
                PlatformInvokeGDI32.DeleteDC(hMemDC);

                //Release the screen device context
                PlatformInvokeUSER32.ReleaseDC(hwnd, hDC);

                //Create image
                Bitmap bmp = System.Drawing.Image.FromHbitmap(hBitmap);

                //Release memory to avoid leaks
                PlatformInvokeGDI32.DeleteObject(hBitmap);

                //Run garbage collector manually
                GC.Collect();

                //Return the bitmap
                return bmp;

            }

            else
            {

                return null;

            }

        }

        #endregion

    }

Thanks R.

+3
source share
2 answers

BitBlt StretchBlt.

+1

, , blitting , , , . .

( , ), 1200x1200 120x120 , , 1200x1200 , . , .

+1

Source: https://habr.com/ru/post/1763900/


All Articles