/// CaptureScreen是完成屏幕截取的类
public class CaptureScreen
{
#region Public Class Functions
public static Bitmap GetDesktopImage()
{
IntPtr m_HBitmap;
System.Drawing.Bitmap bmp=null;
SIZE size;
IntPtr hDC = PlatformInvokeUSER32.GetDC(PlatformInvokeUSER32.GetDesktopWindow());
IntPtr hMemDC = PlatformInvokeGDI32.CreateCompatibleDC(hDC);
size.cx = PlatformInvokeUSER32.GetSystemMetrics (PlatformInvokeUSER32.SM_CXSCREEN);
size.cy = PlatformInvokeUSER32.GetSystemMetrics(PlatformInvokeUSER32.SM_CYSCREEN);
m_HBitmap = PlatformInvokeGDI32.CreateCompatibleBitmap(hDC, size.cx, size.cy);
if (m_HBitmap!=IntPtr.Zero)
{
IntPtr hOld = (IntPtr) PlatformInvokeGDI32.SelectObject(hMemDC, m_HBitmap);
PlatformInvokeGDI32.BitBlt(hMemDC, 0, 0,size.cx,size.cy, hDC, 0, 0,PlatformInvokeGDI32.SRCCOPY);
PlatformInvokeGDI32.SelectObject(hMemDC, hOld);
PlatformInvokeGDI32.DeleteDC(hMemDC);
PlatformInvokeUSER32.ReleaseDC(PlatformInvokeUSER32.GetDesktopWindow(), hDC);
bmp=System.Drawing.Image.FromHbitmap(m_HBitmap);
PlatformInvokeGDI32.DeleteObject(m_HBitmap);
return bmp;
}
return null;
}
#endregion
}
//该结构控制截取屏幕的大小
public struct SIZE
{
public int cx;
public int cy;
}
}
这样就可以效果很好地实现C#屏幕截取编程了。
5 总结
C#屏幕截取编程需要使用很多API编程,使用单独封装的cs文件,即可采用“分而治之”的策略降低编程难度。
|