Exemple #1
0
        private static Bitmap CaptureScreenRegion(Rectangle crop)
        {
            Rectangle totalSize = Rectangle.Empty;

            foreach (Screen s in Screen.AllScreens)
            {
                totalSize = Rectangle.Union(totalSize, s.Bounds);
            }

            IntPtr hSrc  = WindowsApi.CreateDC("DISPLAY", null, null, 0);
            IntPtr hDest = WindowsApi.CreateCompatibleDC(hSrc);
            IntPtr hBmp  = WindowsApi.CreateCompatibleBitmap(hSrc,
                                                             crop.Right -
                                                             crop.Left,
                                                             crop.Bottom -
                                                             crop.Top);
            IntPtr hOldBmp = WindowsApi.SelectObject(hDest, hBmp);

            WindowsApi.BitBlt(hDest, 0, 0, crop.Right - crop.Left,
                              crop.Bottom - crop.Top, hSrc, crop.Left, crop.Top,
                              CopyPixelOperation.SourceCopy |
                              CopyPixelOperation.CaptureBlt);
            Bitmap bmp = Image.FromHbitmap(hBmp);

            WindowsApi.SelectObject(hDest, hOldBmp);
            WindowsApi.DeleteObject(hBmp);
            WindowsApi.DeleteDC(hDest);
            WindowsApi.DeleteDC(hSrc);

            return(bmp.Clone(new Rectangle(0, 0, bmp.Width, bmp.Height),
                             PixelFormat.Format24bppRgb));
        }