0

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 Answers1

0

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

Bartosz Olchowik
  • 1,129
  • 8
  • 22
  • Does System.Drawing not create meta data for the bitmap? I never got the Estimating DPi to be issue, only the warning that the DPI provided is invalid, but it does seem to be something Tesseract calculated on its own, I am not sure how can I override the calculation because there is only a getter for the DPI – LaysYogurt Aug 03 '23 at 07:34
  • @LaysYogurt just create required metadata as it was described in topic on github. I would suggest to just fit into the rules, instead trying to bypass it. An example how to do that is shown in microsoft documentation page: https://learn.microsoft.com/en-us/dotnet/desktop/wpf/graphics-multimedia/how-to-write-metadata-to-a-bitmap?view=netframeworkdesktop-4.8 PS. You could also save the image on disk, and load it after. There is a high probability that in this way metadata will be created. – Bartosz Olchowik Aug 03 '23 at 08:02
  • @LaysYogurt and replying to your question: No, System.Drawing is not generating metadata for the bitmap automatically. – Bartosz Olchowik Aug 03 '23 at 08:12