错误:内存不足,图像到文本。 (C#) [英] Error: Insufficient memory, image to text. (C#)

查看:118
本文介绍了错误:内存不足,图像到文本。 (C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想为图片制作图片。我想从图像中找到文本(oCr)。但它给出了一个错误。 记忆力不足



我的代码:



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:

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:

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

推荐答案

我看到两个问题,第一个:



I see two problems, first:

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





OCR很贵,什么你正在做(使用GetPixel)是非常非常昂贵的,即使你刚刚回来一张图片。那你为什么两次做同样的事呢?您可以为图像指定Null,只需执行以下操作:





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:

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





并且省略如果声明。如果您需要告诉用户,您可以查看AFTER,例如





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

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





接下来是这一行,可能是你的问题来自哪里:



Next is this line, which may be where your problem is coming from:

var img2 = bimg.Clone(new Rectangle(x, y, bimg.Height-y-1, bimg.Width-x-1), pictureBox3.Image.PixelFormat);





矩形定义为:(x,y,Width,Height)。您试图将其定义为(x,y,Height,Width),这是错误的。



如果查看系统的文档.Drawing.Bitmap.Clone() [ ^ ]如果矩形的宽度/高度超出原始位图的边界,您可以看到它给出OutOfMemoryException。



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.


这篇关于错误:内存不足,图像到文本。 (C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆