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
Dispose()to guarantee its disposal?Vision.DetectSkindo? Especially, whyref bitmap2notbitmap2is used? Seems thatDetectSkindoesn't preservebitmap2and returns some other bitmap; in that casebitmapvariable is not disposing.Vision.DetectSkinmethod is called then you really need to include that code.