[DllImport("gdi32.dll")]//在两个上下文中复制图像.
private static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
[DllImport("user32.dll")]//获取Desktop句柄
private static extern IntPtr GetDesktopWindow();
private PaintScreen()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
//FileName:保存于指定的文件中.
//Format:指定文件格式.如:jpeg bmp 等等
public static void ps(string FileName,ImageFormat Format)//保存屏幕图像.
{
Bitmap MemoryImage;//在内存中保存屏幕图像.
//获取屏幕的高度,宽度
Size ScreenSize = new Size (Screen.PrimaryScreen .Bounds .Width,Screen.PrimaryScreen .Bounds .Height);
IntPtr Desktop = GetDesktopWindow();//获取Desktop句柄
Graphics Desktops = Graphics.FromHwnd (Desktop);//从句柄转换到图像对象.
MemoryImage = new Bitmap(ScreenSize.Width ,ScreenSize.Height,Desktops);//创建一个同屏幕高,宽一样的位图对象
Graphics MemoryGraphics = Graphics.FromImage(MemoryImage);//取得它的图像对象.
IntPtr Src = Desktops.GetHdc();//取得桌面的HDC
IntPtr Dest = MemoryGraphics.GetHdc();//取得内存位图的HDC
BitBlt(Dest, 0, 0, ScreenSize.Width ,ScreenSize.Height,Src, 0, 0, 13369376);//进行复制
Desktops.ReleaseHdc(Src);//必须释放
MemoryGraphics.ReleaseHdc(Dest);//必须释放
MemoryImage.Save (FileName,Format);//保存图像
}