Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建位图C时发生OutOfMemory异常#_C#_.net_Image Processing_Bitmap - Fatal编程技术网

C# 创建位图C时发生OutOfMemory异常#

C# 创建位图C时发生OutOfMemory异常#,c#,.net,image-processing,bitmap,C#,.net,Image Processing,Bitmap,所以,如果我错了,请纠正我,因为已经有一个复制品出现了——但我在过去的几个小时里,在堆栈中拖网,绞尽脑汁,我似乎无法用我的一生来解决这个问题 我已经编写了一个基本的、单线程的、递归的文件爬行系统,该系统将查找它能找到的任何图像文件,并将其路径加载到一个数组中。然后将数组传递给一个方法,该方法遍历数组并检查每个图像的大小(H,W)——如果它满足最低要求,则将其保存到一个新的最终数组,如果它不满足,则忽略它 我已尝试使用USING语句创建所有位图,以确保创建的垃圾尽可能少。。。然而,我仍然有内存不足

所以,如果我错了,请纠正我,因为已经有一个复制品出现了——但我在过去的几个小时里,在堆栈中拖网,绞尽脑汁,我似乎无法用我的一生来解决这个问题

我已经编写了一个基本的、单线程的、递归的文件爬行系统,该系统将查找它能找到的任何图像文件,并将其路径加载到一个数组中。然后将数组传递给一个方法,该方法遍历数组并检查每个图像的大小(H,W)——如果它满足最低要求,则将其保存到一个新的最终数组,如果它不满足,则忽略它

我已尝试使用USING语句创建所有位图,以确保创建的垃圾尽可能少。。。然而,我仍然有内存不足的异常。以下是我的代码片段:

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();
    }
}
出错并引发异常的行是:

using (Bitmap bitmap = new Bitmap(current))
这很有趣,因为当Vision.DetectSkin方法未被调用时,程序可以工作。然而,在完成文件爬网和缩放处理后,只有当Vision类没有注释掉时,有问题的行才会抛出错误


无论如何,我们将非常感谢您的帮助!提前感谢

如果您提到Vision.DetectSkin来自

下面是有问题的代码,为了简洁起见删除了注释。请注意,它在第一行生成一个
图形
对象,但根本不使用它<代码>图形实现了<代码>IDisposable但它没有被处理;换言之,代码将位图加载到另一种格式,对它不做任何处理,然后不处理它。我会试着删除那行,看看你的问题是否消失了

仅仅因为它在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);
                }
            }
        }
    }
publicstaticvoiddetectskin(位图原始,ref位图修改)
{
Graphics g=Graphics.FromImage(原件);
ArrayList points=新的ArrayList();
对于(Int32 x=0;x如果(I=4&&hue,如果您提到Vision.DetectSkin
的来源,那会很有帮助

这里是有问题的代码,为了简洁起见删除了注释。请注意,它在第一行生成了一个
Graphics
对象,但它根本没有被使用。
Graphics
实现了
IDisposable
,但它没有被处理;换句话说,代码正在将位图加载到另一种格式,对它不做任何操作,并且然后不处理它。我会尝试删除那行,看看你的问题是否消失

仅仅因为它在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);
                }
            }
        }
    }
publicstaticvoiddetectskin(位图原始,ref位图修改)
{
Graphics g=Graphics.FromImage(原件);
ArrayList points=新的ArrayList();
对于(Int32 x=0;x如果(I=4&&hue,如果您提到Vision.DetectSkin
的来源,那会很有帮助

这里是有问题的代码,为了简洁起见删除了注释。请注意,它在第一行生成了一个
Graphics
对象,但它根本没有被使用。
Graphics
实现了
IDisposable
,但它没有被处理;换句话说,代码正在将位图加载到另一种格式,对它不做任何操作,并且然后不处理它。我会尝试删除那行,看看你的问题是否消失

仅仅因为它在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);
                }
            }
        }
    }
publicstaticvoiddetectskin(位图原始,ref位图修改)
{
Graphics g=Graphics.FromImage(原件);
ArrayList points=新的ArrayList();
对于(Int32 x=0;x如果(I=4&&hue,如果您提到Vision.DetectSkin
的来源,那会很有帮助

这里是有问题的代码,为了简洁起见删除了注释。请注意,它在第一行生成了一个
Graphics
对象,但它根本没有被使用。
Graphics
实现了
IDisposable
,但它没有被处理;换句话说,代码正在将位图加载到另一种格式,对它不做任何操作,并且然后不处理它。我会尝试删除那行,看看你的问题是否消失

仅仅因为它在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);
                }
            }
        }
    }
publicstaticvoiddetectskin(位图原始,ref位图修改)
{
Graphics g=Graphics.FromImage(原件);
阵列列表点=