简体   繁体   English

创建位图C#时出现OutOfMemory异常

[英]OutOfMemory Exception when creating a bitmap C#

so correct me if im wrong and there's already a duplicate out there - but i have spent the past few hours trawling through stack and racking my brain and i cant for the life of me seem to fix this. 因此,如果我做错了,请纠正我,那里已经有重复的东西了-但是我花了过去几个小时在堆栈中拖拉并绞尽脑汁,我无法为自己的一生解决这个问题。

I have written a basic, single threaded, recursive file crawling system that will look for any image file it can find and load it's path into an array. 我编写了一个基本的单线程递归文件爬网系统,它将查找它可以找到的任何图像文件并将其路径加载到数组中。 Then the array is passed to a method that iterates through the array and checks the size (H,W) of each image - If it meets the minimum requirements then it saves it to a new, final array, and if it doesnt, it is simply ignored. 然后,将数组传递给迭代该数组的方法,并检查每个图像的大小(H,W)-如果满足最低要求,则将其保存到新的最终数组中,如果不满足,则为简单地忽略了。

I have tried to create all of my Bitmaps with the USING statement to ensure as little garbage as possible is created... however, im still getting out of memory exceptions. 我尝试使用USING语句创建所有位图,以确保创建尽可能少的垃圾...但是,即时消息仍然会出现内存不足的异常。 Here is a snippet of my code: 这是我的代码片段:

foreach (string current in scaledList)
{
    using (Bitmap bitmap = new Bitmap(current))
    {
        Bitmap bitmap2 = bitmap;
        float num5 = (float)(bitmap.Width / num2 * (bitmap.Height / num2));
        float num6 = (float)Vision.DetectSkin(bitmap, ref bitmap2, num2, iValue, hueMin, hueMax);
        num7 = num6 / num5 * 100f;
        bitmap2.Dispose();
    }
}

The line that is bugging out and throwing the exception is: 出现错误并抛出异常的行是:

using (Bitmap bitmap = new Bitmap(current))

which is interesting given that the program works when the Vision.DetectSkin method isnt called. 鉴于该程序在不调用Vision.DetectSkin方法时可以运行,因此很有趣。 however - upon completion of the file crawling and scale processing, it is only when the Vision class isnt commented out will the offending line throw the error. 但是-在完成文件爬网和缩放处理后,只有在Vision类未注释掉的情况下,有问题的行才会引发错误。

Anywyas, all help would be greatly appreciated! Anywyas,所有帮助将不胜感激! Thanks in advance 提前致谢

It would have been helpful if you mentioned that Vision.DetectSkin came from http://www.codeproject.com/Articles/8127/Skin-Recognition-in-C . 如果您提到Vision.DetectSkin来自http://www.codeproject.com/Articles/8127/Skin-Recognition-in-C ,这将很有帮助。

Here's the code in question, with comments removed for brevity. 这是有问题的代码,为简洁起见删除了注释。 Note that it makes a Graphics object on the first line, but it is not used at all. 请注意,它在第一行上创建了一个Graphics对象,但根本没有使用。 Graphics implements IDisposable but it's not being disposed; Graphics实现了IDisposable但是没有被废弃。 in other words, the code is loading up the bitmap into another format, doing nothing with it, and then not disposing it. 换句话说,代码将位图加载为另一种格式,对此不执行任何操作,然后不进行处理。 I would try deleting that line and see if your problems go away. 我会尝试删除该行,看看您的问题是否消失了。

Just because it's on CodeProject doesn't mean it's good, tested, and debugged code... 仅仅因为它在CodeProject上并不意味着它是好的,经过测试和调试的代码...

    public static void DetectSkin(Bitmap original, ref Bitmap modified)
    {
        Graphics g = Graphics.FromImage(original);
        ArrayList points = new ArrayList();
        for (Int32 x = 0; x < original.Width; x++)
        {
            for (Int32 y = 0; y < original.Height; y++)
            {
                Color c = modified.GetPixel(x, y);

                double I = (Math.Log(c.R) + Math.Log(c.B) + Math.Log(c.G)) / 3;
                double Rg = Math.Log(c.R) - Math.Log(c.G);
                double By = Math.Log(c.B) - (Math.Log(c.G) + Math.Log(c.R)) / 2;
                double hue = Math.Atan2(Rg, By) * (180 / Math.PI);

                if (I <= 5 && (hue >= 4 && hue <= 255))
                {
                    points.Add(new Point(x, y));
                }
                else
                {
                    modified.SetPixel(x, y, Color.Black);
                }
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM