c# memory leak

pandamonium99

Newcomer
Joined
Mar 3, 2012
Im trying to create a pixel search function, but i keep getting memory leaks and i cant figure out why. I fixed 1 put another popped up. Was wondering what im doing wrong.
Code:
        }
        public Bitmap capturescreen(Rectangle rect)
        {
            Size sz = Screen.PrimaryScreen.Bounds.Size;
            IntPtr hDesk = GetDesktopWindow();
            IntPtr hSrce = GetWindowDC(hDesk);
            IntPtr hDest = CreateCompatibleDC(hSrce);
            IntPtr hBmp = CreateCompatibleBitmap(hSrce, sz.Width, sz.Height);
            IntPtr hOldBmp = SelectObject(hDest, hBmp);
            bool b = BitBlt(hDest, 0, 0, rect.Width, rect.Height, hSrce, 0, 0, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt);
            Bitmap bmp = Bitmap.FromHbitmap(hBmp); // <-------- Says out of memory on this line.
            SelectObject(hDest, hOldBmp);
            DeleteObject(hBmp);
            DeleteDC(hDest);
            ReleaseDC(hDesk, hSrce);
            
            
            return bmp;
        
        }
    }
}
Here is the pixel search function which uses the function above
Code:
        public Point PixelSearch(Rectangle rect, int PixelColor, int Shade_Variation)
        {
            Color Pixel_Color = Color.FromArgb(PixelColor);

           
            Point Pixel_Coords = new Point(-1, -1);
            Bitmap RegionIn_Bitmap = capturescreen(rect);
            BitmapData RegionIn_BitmapData = RegionIn_Bitmap.LockBits(new Rectangle(0, 0, RegionIn_Bitmap.Width, RegionIn_Bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            int[] Formatted_Color = new int[3] { Pixel_Color.B, Pixel_Color.G, Pixel_Color.R }; //bgr
            
            unsafe
            {
                for (int y = 0; y < RegionIn_BitmapData.Height; y++)
                {
                    byte* row = (byte*)RegionIn_BitmapData.Scan0 + (y * RegionIn_BitmapData.Stride);

                    for (int x = 0; x < RegionIn_BitmapData.Width; x++)
                    {
                        if (row[x * 3] >= (Formatted_Color[0] - Shade_Variation) & row[x * 3] <= (Formatted_Color[0] + Shade_Variation)) //blue
                        {
                            if (row[(x * 3) + 1] >= (Formatted_Color[1] - Shade_Variation) & row[(x * 3) + 1] <= (Formatted_Color[1] + Shade_Variation)) //green
                            {
                                if (row[(x * 3) + 2] >= (Formatted_Color[2] - Shade_Variation) & row[(x * 3) + 2] <= (Formatted_Color[2] + Shade_Variation)) //red
                                {
                                    Pixel_Coords = new Point(x + rect.X, y + rect.Y);
                                    goto end;
                                }
                            }
                        }
                    }
                }
            }

        end:
            RegionIn_Bitmap.UnlockBits(RegionIn_BitmapData);
            return Pixel_Coords;
        }
 

dotnetguy37

Freshman
Joined
Nov 15, 2009
Re: c# Capture Screen and Pixel Search

I see you also cross posted to CodeGuru (so this must be really important to you):
http://www.codeguru.com/forum/showthread.php?p=2058440

The question is/becomes: Which code routine is causing the leak - the screencapture or the pixel search?

Are you using any leak detection tools like:
ANTS Memory Profiler 7.2

If you don't have access to any such leak detection tools the best troubleshooting method is to find code to do each separately,
insure that neither routine is leaking, then try to merge them.
Capture a Screen Shot


It looks like the pixel search function you posted came from here.
I would take a step back and look at writing your own pixel search routine.

The LockBits function has better performance than GetPixel but can be somewhat more involved to implement.

Here's a good overview:
Using the LockBits method to access image data

Surprisingly enough MSDN actually has an actual example of how it recommends using LockBits
(which might be a good starting point for a pixelsearch class/routine since
Microsoft is not in the habit of release code examples with memory leaks included).
The example is at the bottom of this page.

When I ended up developing a screencapture utility of my own I ended up starting from using the C# source code for Cropper.

Some other code for looping through LockBits pixels to search for a color can be found here and here.
 
Last edited:

snarfblam

Ultimate Contributor
Joined
Jun 10, 2003
Location
USA
Re: c# Capture Screen and Pixel Search

Have you examined your process' and machine's actual memory usage when this error occurs? GDI and GDI+ seem to derive joy from throwing inexplicable OutOfMemoryExceptions when there is a completely unrelated problem. Your issue may not have anything to do with memory at all.

Try using a different image format, or, if you are trying to access a resource, see if the same code works with an external file. If you're accessing a network drive, try a local drive. Maybe you can narrow it down.

If you do have an actual memory leak, there are free profilers available, which are great for many things, including finding memory issues.
 
Last edited:
Top Bottom