public Bitmap CaptureWindow(IntPtr hwnd) { IntPtr desktopHwnd = UnsafeNativeMethods.GetDesktopWindow(); IntPtr desktopDC = UnsafeNativeMethods.GetWindowDC(desktopHwnd); IntPtr windowDC = UnsafeNativeMethods.GetWindowDC(hwnd); Rectangle windowRegion = Interop.AdjustWindowRectangeToDesktopBounds(Interop.GetAbsoluteClientRect(hwnd)); IntPtr memoryDC = UnsafeNativeMethods.CreateCompatibleDC(desktopDC); IntPtr hBitmap = UnsafeNativeMethods.CreateCompatibleBitmap(desktopDC, windowRegion.Width, windowRegion.Height); IntPtr holdBitmap = UnsafeNativeMethods.SelectObject(memoryDC, hBitmap); Bitmap b = null; bool success = UnsafeNativeMethods.BitBlt (memoryDC, 0, 0, windowRegion.Width, windowRegion.Height, desktopDC, windowRegion.Left, windowRegion.Top, UnsafeNativeMethods.SRCCOPY | UnsafeNativeMethods.CAPTUREBLT); if (success) { b = Image.FromHbitmap(hBitmap); } UnsafeNativeMethods.SelectObject(memoryDC, holdBitmap); UnsafeNativeMethods.DeleteObject(hBitmap); UnsafeNativeMethods.DeleteDC(memoryDC); UnsafeNativeMethods.ReleaseDC(desktopHwnd, desktopDC); UnsafeNativeMethods.ReleaseDC(hwnd, windowDC); return(b); }
/// <summary> /// Capture the entire client area of a window /// </summary> /// <param name="hWnd"></param> /// <returns></returns> public Bitmap CaptureWindow(IntPtr hWnd) { return(CaptureRegionDirect3D(hWnd, Interop.AdjustWindowRectangeToDesktopBounds(Interop.GetAbsoluteClientRect(hWnd)))); }