Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello, i want to make an image to a text. And i want to find the text from the image (oCr). But it gives an error. "insufficient memory"

My code:

C#
private Image DetectNumbers2(Image img, Color c)
    {
        try
        {
            Bitmap bimg = new Bitmap(img);
            for (int x = 0; x < bimg.Width - 1; x++)
            {
                for (int y = 0; y < bimg.Height - 1; y++)
                {
                    Color c2 = bimg.GetPixel(x, y);
                    if (c2.R >= 240 && c2.R <= 255 && c2.G >= 45 && c2.G <= 60 && c2.B >= 45 && c2.B <= 60)
                    {
                        var img2 = bimg.Clone(new Rectangle(x, y, bimg.Height-y-1, bimg.Width-x-1), pictureBox3.Image.PixelFormat);
                        return img;
                    }
                }
            }
            return null;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            return null;
        }
    }


My button:

C#
if (DetectNumbers2(pictureBox6.Image, GetReadColor(int.Parse(label1.Text))) != null) pictureBox11.Image = DetectNumbers2(pictureBox6.Image, GetReadColor(int.Parse(label1.Text)));
Posted
Comments
Sergey Alexandrovich Kryukov 8-Oct-13 13:08pm    
In what line? And I see no trace of using any OCR. Where is it, what is it?
—SA

1 solution

I see two problems, first:

C#
if (DetectNumbers2(pictureBox6.Image, GetReadColor(int.Parse(label1.Text))) != null) pictureBox11.Image = DetectNumbers2(pictureBox6.Image, GetReadColor(int.Parse(label1.Text)));


OCR is expensive, and what you are doing (using GetPixel) is VERY VERY expensive, even if you are just returning a piece of an image. So why are you doing the exact same thing twice? You can assign Null to an image, just do this:

C#
pictureBox11.Image = DetectNumbers2(pictureBox6.Image, GetReadColor(int.Parse(label1.Text)));


And leave out the If statement. If you need to tell the user, you can check AFTER, like

C#
if (pictureBox11.Image == null)
  //Tell the user that it failed....


Next is this line, which may be where your problem is coming from:
C#
var img2 = bimg.Clone(new Rectangle(x, y, bimg.Height-y-1, bimg.Width-x-1), pictureBox3.Image.PixelFormat);


Rectangles are defined as : (x, y, Width, Height). You are trying to define it as (x, y, Height, Width), which is wrong.

If you look at the documentation for System.Drawing.Bitmap.Clone()[^] You can see it gives an OutOfMemoryException if the width/height of the rectangle is outside of the bounds of the original bitmap.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Oct-13 16:39pm    
GetPixel? Wow, well spotted, a 5.
—SA
Ron Beyer 8-Oct-13 17:03pm    
Thanks Sergey

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900