public static Bitmap CaptureWindowDirect(IntPtr wnd, WindowSettings settings, out SIZE size, out Win32Stuff.WINDOWINFO info) { IntPtr hBitmap; IntPtr hDC = Win32Stuff.GetDC(wnd); IntPtr hMemDC = GDIStuff.CreateCompatibleDC(hDC); info = new Win32Stuff.WINDOWINFO(); info.cbSize = (uint)Marshal.SizeOf(info); Win32Stuff.GetWindowInfo(wnd, ref info); size.cx = Math.Max(1, info.rcClient.Width + settings.offsetRight - settings.offsetLeft); // Win32Stuff.GetSystemMetrics(Win32Stuff.SM_CXSCREEN); size.cy = Math.Max(1, info.rcClient.Height + settings.offsetBottom - settings.offsetTop); // Win32Stuff.GetSystemMetrics(Win32Stuff.SM_CYSCREEN hBitmap = GDIStuff.CreateCompatibleBitmap(hDC, size.cx, size.cy); if (hBitmap != IntPtr.Zero) { IntPtr hOld = (IntPtr)GDIStuff.SelectObject(hMemDC, hBitmap); GDIStuff.BitBlt(hMemDC, 0, 0, size.cx, size.cy, hDC, settings.offsetLeft, settings.offsetTop, GDIStuff.SRCCOPY); GDIStuff.SelectObject(hMemDC, hOld); GDIStuff.DeleteDC(hMemDC); Win32Stuff.ReleaseDC(Win32Stuff.GetDesktopWindow(), hDC); Bitmap bmp = System.Drawing.Image.FromHbitmap(hBitmap); GDIStuff.DeleteObject(hBitmap); GC.Collect(); return(bmp); } return(null); }
public static Bitmap CaptureDesktop(int x = 0, int y = 0, int wid = -1, int hei = -1) { SIZE size; IntPtr hBitmap; IntPtr hDC = Win32Stuff.GetDC(Win32Stuff.GetDesktopWindow()); IntPtr hMemDC = GDIStuff.CreateCompatibleDC(hDC); size.cx = wid == -1 ? Win32Stuff.GetSystemMetrics(Win32Stuff.SM_CXSCREEN) : wid; size.cy = hei == -1 ? Win32Stuff.GetSystemMetrics(Win32Stuff.SM_CYSCREEN) : hei; hBitmap = GDIStuff.CreateCompatibleBitmap(hDC, size.cx, size.cy); if (hBitmap != IntPtr.Zero) { IntPtr hOld = (IntPtr)GDIStuff.SelectObject(hMemDC, hBitmap); GDIStuff.BitBlt(hMemDC, 0, 0, size.cx, size.cy, hDC, x, y, GDIStuff.SRCCOPY); GDIStuff.SelectObject(hMemDC, hOld); GDIStuff.DeleteDC(hMemDC); Win32Stuff.ReleaseDC(Win32Stuff.GetDesktopWindow(), hDC); Bitmap bmp = System.Drawing.Image.FromHbitmap(hBitmap); GDIStuff.DeleteObject(hBitmap); GC.Collect(); return(bmp); } return(null); }