public static Bitmap CaptureRectangle(Rectangle captureBounds) { IntPtr hDesktop = User32.GetDesktopWindow(); IntPtr hDC = User32.GetWindowDC(hDesktop); IntPtr hDest = GDI32.CreateCompatibleDC(hDC); IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hDC, captureBounds.Width, captureBounds.Height); IntPtr hOldBitmap = GDI32.SelectObject(hDest, hBitmap); GDI32.BitBlt(hDest, 0, 0, captureBounds.Width, captureBounds.Height, hDC, captureBounds.X, captureBounds.Y, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt); Bitmap bitmap = null; bool isRegionEmpty = true; using (var graphics = Graphics.FromHwnd(hDesktop)) { isRegionEmpty = WindowCapture.IsRegionEmpty(graphics, captureBounds); } if (isRegionEmpty) { bitmap = Bitmap.FromHbitmap(hBitmap); } else { float xDpi = 96F, yDpi = 96F; using (var tmp = Bitmap.FromHbitmap(hBitmap)) { xDpi = tmp.HorizontalResolution; yDpi = tmp.VerticalResolution; } bitmap = WindowCapture.CreateEmpty(captureBounds.Width, captureBounds.Height, PixelFormat.Format32bppArgb, Color.Transparent, xDpi, yDpi); using (var graphics = Graphics.FromImage(bitmap)) { foreach (var screen in Screen.AllScreens) { var bounds = screen.Bounds; bounds.Offset(-captureBounds.X, -captureBounds.Y); graphics.DrawImage(bitmap, bounds, bounds.X, bounds.Y, bounds.Width, bounds.Height, GraphicsUnit.Pixel); } } } GDI32.SelectObject(hDest, hOldBitmap); GDI32.DeleteObject(hBitmap); GDI32.DeleteDC(hDest); User32.ReleaseDC(hDesktop, hDC); return(bitmap); }
public static Bitmap CaptureWindow() { Bitmap bitmap = null; try { var captureBounds = Utils.GetScreenBounds(); bitmap = WindowCapture.CaptureRectangle(captureBounds); } catch (Exception ex) { // TODO: handle exception Debug.WriteLine(ex.Message); throw; } return(bitmap); }