DPI of screenshot taken using Graphics changes to 3700 after use in TesseractOCR

115 Views Asked by At

I am using c# winforms and I am taking a screenshot using Graphics.copyfromscreen

private Bitmap CaptureImage()
        {
            // Create a bitmap with the specified dimensions
            Bitmap capturedImage = new Bitmap(captureArea.Width, captureArea.Height);

            // Create a graphics object from the bitmap
            using (Graphics graphics = Graphics.FromImage(capturedImage))
            {

            graphics.InterpolationMode =
      System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

                // Copy the screen pixels to the bitmap
            graphics.CopyFromScreen(captureArea.Location, Point.Empty, captureArea.Size);
            }

            // Store the captured image for later use
            // You can save it as a class member or in a desired location
            // In this example, it's being stored as a class member
            return capturedImage;
        }

I am using this image in IronOcr/TesseractOcr, but I get the following warning:

Warning: Invalid resolution 3780 dpi. Using 70 instead.

This library requires atleast 300 dpi to run accurately, I am getting some bad results due to low DPI, how can I fix this?

I tried changing the input.TargetDPi of OCRInput inside of IronOcr, but it is clear that the problem is with the image because if I save a normal screen shot to a file and then use it this warning does not come, I tried changing the DPI of the image using .SetResolution after using .copyfromscreen aswell but no change at all.

1

There are 1 best solutions below

3
On

The reason of that warning is that your image does not contain a resolution info in its metadata- System.Drawing is not creating metadata for images automaticaly. When you save a screenshot with Paint, or any other tool, it probably generates metadata that contains resolution info. If there is lack of metadata in given image to tessarect, it tries to calculate the resolution, so you get warning. I dont want to copy-paste solution from another website, so i will give u a link to solution located on github:

Github: Possible solution of your problem with detailed explanation