[DllImport("coredll.dll", EntryPoint = "BitBlt")]
extern public static bool BitBlt(
IntPtr hdcDest, // 目标 DC的句柄
int nXDest,
int nYDest,
int nWidth,
int nHeight,
IntPtr hdcSrc, // 源DC的句柄
int nXSrc,
int nYSrc,
RasterOperation dwRop // 光栅的处理数值
);
[DllImport("coredll.dll", EntryPoint = "GetDC")]
public static extern IntPtr GetDC(IntPtr hwnd);
//截取屏幕图像
public static Image GetScreenShot()
{
try
{
IntPtr srcHdc = GetDC(IntPtr.Zero);
Rectangle rect = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
Bitmap image = new Bitmap(rect.Width, rect.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
using (Graphics graphics = Graphics.FromImage(image))
{
IntPtr dstHdc = graphics.GetHdc();
BitBlt(dstHdc, 0, 0, image.Width, image.Height, srcHdc, 0,0, 0x00CC0020);
graphics.ReleaseHdc(dstHdc);
return image;
}
}
catch (System.Exception e)
{
MessageBox.Show(e.Message, "GetScreenShot Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
return null;
}
}