public void Blit(User32DeviceContext hdcSrc, GDI32DeviceContext hdcDest) { // select the bitmap object IntPtr hOld = new IntPtr(); try { hOld = GDI32.SelectObject(hdcDest.DC, hBitmap); // bitblt over GDI32.BitBlt( hdcDest.DC, 0, 0, size.Width, size.Height, hdcSrc.DC, 0, 0, GDI32.SRCCOPY ); } finally { // restore selection if (hOld.ToInt32() != 0) { GDI32.SelectObject(hdcDest.DC, hOld); } } }
void IDisposable.Dispose() { this.hdcSrc = null; try { // clean up GDI32.DeleteObject(this.hBitmap); this.hBitmap = new IntPtr(); } catch (Exception) { } }
void IDisposable.Dispose() { this.hdcSrc = null; try { // clean up GDI32.DeleteDC(this.dc); this.dc = new IntPtr(); } catch (Exception) { } }
/// <summary> /// Creates an Image object containing a screen shot of a specific window /// </summary> /// <param name="handle">The handle to the window. (In windows forms, this is obtained by the Handle property)</param> /// <returns></returns> public static Image Capture(IntPtr handle) { // get te hDC of the target window using (User32DeviceContext hdcSrc = new User32DeviceContext(handle)) { Size size = GetSize(handle); using (GDI32DeviceContext hdcDest = new GDI32DeviceContext(hdcSrc)) { // create a bitmap we can copy it to, // using GetDeviceCaps to get the width/height using (GDI32HBitmap bmp = new GDI32HBitmap(hdcSrc, size)) { bmp.Blit(hdcSrc, hdcDest); return(bmp.GetImage()); } } } }
public GDI32HBitmap(User32DeviceContext hdcSrc, Size size) { this.hdcSrc = hdcSrc; this.size = size; this.hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc.DC, size.Width, size.Height); }
public GDI32DeviceContext(User32DeviceContext hdcSrc) { this.hdcSrc = hdcSrc; this.dc = GDI32.CreateCompatibleDC(hdcSrc.DC); }