0

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.

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. 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. 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.

Anywyas, all help would be greatly appreciated! Thanks in advance

5
  • What if you include the declaration of the second bitmap in a using as well, instead of explicitly calling Dispose() to guarantee its disposal? Commented Jul 31, 2015 at 16:50
  • what does Vision.DetectSkin do? Especially, why ref bitmap2 not bitmap2 is used? Seems that DetectSkin doesn't preserve bitmap2 and returns some other bitmap; in that case bitmap variable is not disposing. Commented Jul 31, 2015 at 16:51
  • using statement is for disposing un-managed objects, creating more garbage. It does not reduce garbage in anyway :p Commented Jul 31, 2015 at 17:03
  • If the problem only occurs when the Vision.DetectSkin method is called then you really need to include that code. Commented Jul 31, 2015 at 17:05
  • Fixed it guys! Thankyou for the input but as Paul said, the problem was not with my code - but in the DetectSkin method which i've inherited for this project from a previous programmer... didnt even know that shit was opensourced!! haha Commented Jul 31, 2015 at 17:22

1 Answer 1

3

It would have been helpful if you mentioned that Vision.DetectSkin came from 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 implements IDisposable but it's not being disposed; 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...

    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);
                }
            }
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

I didnt even know the Vision code came from online!! I have been looking for it - but i've inherited it with this project from a previous programmer haha. thanks for the help - will implement the changes now and see what happens
That makes sense, given your function has a few extra parameters. It's definitely where the problem lies so if you still have issues, you might want to post your source to DetectSkin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.