[DllImport( "gdi32.dll", CharSet = CharSet.Auto,
SetLastError = true, ExactSpelling = true )]
public static extern int BitBlt(
HandleRef hDC, int x, int y, int nWidth, int nHeight,
HandleRef hSrcDC, int xSrc, int ySrc, int dwRop );
public static void CopyFromScreen(
Graphics gfx, int xSource, int ySource,
int xDest, int yDest, Size blockRegionSize ) {
HandleRef src, dest;
Graphics hDCDesktop = Graphics.FromHwnd( IntPtr.Zero );
try {
dest = new HandleRef( null, gfx.GetHdc() );
src = new HandleRef( null, hDCDesktop.GetHdc() );
int result = BitBlt(
dest, xDest, yDest,
blockRegionSize.Width, blockRegionSize.Height,
src, xSource, ySource, 0xcc0020 );
if ( result == 0 ) { throw new Exception(); }
} finally {
if ( dest.Handle != IntPtr.Zero )
gfx.ReleaseHdc();
if ( src.Handle != IntPtr.Zero )
hDCDesktop.ReleaseHdc();
}
}