I keep getting memory leaks from GDI+ and i cant figure out whats doing it. Im trying to make a pixel detection system. I could really use some help finding whatever im doing wrong here >.< Doh!
Here is my first function where it says i ran out of memory.
And here is the pixel search function which uses the above function.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 here SelectObject(hDest, hOldBmp); DeleteObject(hBmp); DeleteObject(hOldBmp); DeleteDC(hDest); ReleaseDC(hDesk, hSrce); return bmp; }
Code:// P/Invoke declarations [DllImport("gdi32.dll")] static extern bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int wDest, int hDest, IntPtr hdcSource, int xSrc, int ySrc, CopyPixelOperation rop); [DllImport("user32.dll")] static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDc); [DllImport("gdi32.dll")] static extern IntPtr DeleteDC(IntPtr hDc); [DllImport("gdi32.dll")] static extern IntPtr DeleteObject(IntPtr hDc); [DllImport("gdi32.dll")] static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight); [DllImport("gdi32.dll")] static extern IntPtr CreateCompatibleDC(IntPtr hdc); [DllImport("gdi32.dll")] static extern IntPtr SelectObject(IntPtr hdc, IntPtr bmp); [DllImport("user32.dll")] public static extern IntPtr GetDesktopWindow(); [DllImport("user32.dll")] public static extern IntPtr GetWindowDC(IntPtr ptr); 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; }




Reply With Quote