Exemple #1
0
        private static Bitmap CaptureWindowArea(IntPtr handle, Point cropPos, Size cropSize)
        {
            IntPtr dc = User32.GetWindowDC(handle);

            if (dc != null)
            {
                IntPtr hdcDest = GDI32.CreateCompatibleDC(dc);
                IntPtr hBitmap = GDI32.CreateCompatibleBitmap(dc, cropSize.Width, cropSize.Height);
                IntPtr hOld    = GDI32.SelectObject(hdcDest, hBitmap);
                if (GDI32.BitBlt(hdcDest, 0, 0, cropSize.Width, cropSize.Height, dc, cropPos.X, cropPos.Y, GDI32.TernaryRasterOperations.SRCCOPY))
                {
                    GDI32.SelectObject(hdcDest, hOld);
                    GDI32.DeleteDC(hdcDest);
                    User32.ReleaseDC(handle, dc);
                    Bitmap b = Image.FromHbitmap(hBitmap);
                    GDI32.DeleteObject(hBitmap);
                    for (int x = 0; x < b.Width; x++)
                    {
                        for (int y = 0; y < b.Height; y++)
                        {
                            if (b.GetPixel(x, y) != Color.FromArgb(255, 0, 0, 0))
                            {
                                return(b);
                            }
                        }
                    }
                    b.Dispose();
                }
                else
                {
                    GDI32.DeleteDC(hdcDest);
                    User32.ReleaseDC(handle, dc);
                    GDI32.DeleteObject(hBitmap);
                }
            }
            return(null);
        }