Code
//1 简易截图方法,不能截取透明窗体
Bitmap screenBitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics g = Graphics.FromImage(screenBitmap);
g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size);
g.Dispose();
//2 使用API截图
/// <summary>
/// Windows API BitBlt 函数
/// </summary>
/// <param name="hdcDest">目标设备的句柄</param>
/// <param name="nXDest">目标对象的左上角的X坐标</param>
/// <param name="nYDest">目标对象的左上角的Y坐标</param>
/// <param name="nWidth">目标对象的矩形的宽度</param>
/// <param name="nHeight">目标对象的矩形的高度</param>
/// <param name="hdcSrc">源设备的句柄</param>
/// <param name="nXSrc">源对象的左上角的X坐标</param>
/// <param name="nYSrc">源对象的左上角的X坐标</param>
/// <param name="dwRop">光栅的操作值,真彩含透明窗体请使用CopyPixelOperation.CaptureBlt | CopyPixelOperation.SourceCopy</param>
/// <returns></returns>
[DllImport("gdi32.dll")]
public static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, System.Drawing.CopyPixelOperation dwRop);
//建立屏幕Graphics
Graphics screenGraphic = Graphics.FromHwnd(IntPtr.Zero);
//根据屏幕大小建立位图,这就是最后截取到的*屏幕图像*
Bitmap screenBitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, screenGraphic);
//建立位图相关Graphics
Graphics bitmapGraphics = Graphics.FromImage(screenBitmap);
//建立屏幕上下文
IntPtr hdcScreen = screenGraphic.GetHdc();
//建立位图上下文
IntPtr hdcBitmap = bitmapGraphics.GetHdc();
//将屏幕捕获保存在位图中
Win32API.BitBlt(hdcBitmap, 0, 0, rectangle.Width, rectangle.Height, hdcScreen, rectangle.X, rectangle.Y, CopyPixelOperation.CaptureBlt | CopyPixelOperation.SourceCopy);
//关闭位图句柄
bitmapGraphics.ReleaseHdc(hdcBitmap);
//关闭屏幕句柄
screenGraphic.ReleaseHdc(hdcScreen);
//将截图尺寸规格化到指定尺寸
Bitmap reSizseBitmap = new Bitmap(rectangle.Width, rectangle.Height);
Graphics reSizeBitmapGraphics = Graphics.FromImage(reSizseBitmap);
reSizeBitmapGraphics.DrawImage(screenBitmap, new Rectangle(0, 0, rectangle.Width, rectangle.Height), new Rectangle(0, 0, rectangle.Width, rectangle.Height), GraphicsUnit.Pixel);
//为释放 Graphics 和由 FromImage 方法创建的相关资源,您应该始终调用 Dispose 方法。
reSizeBitmapGraphics.Dispose();
//释放位图对像
bitmapGraphics.Dispose();
//释放屏幕对像
screenGraphic.Dispose();
screenBitmap是全屏图像
//1 简易截图方法,不能截取透明窗体
Bitmap screenBitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics g = Graphics.FromImage(screenBitmap);
g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size);
g.Dispose();
//2 使用API截图
/// <summary>
/// Windows API BitBlt 函数
/// </summary>
/// <param name="hdcDest">目标设备的句柄</param>
/// <param name="nXDest">目标对象的左上角的X坐标</param>
/// <param name="nYDest">目标对象的左上角的Y坐标</param>
/// <param name="nWidth">目标对象的矩形的宽度</param>
/// <param name="nHeight">目标对象的矩形的高度</param>
/// <param name="hdcSrc">源设备的句柄</param>
/// <param name="nXSrc">源对象的左上角的X坐标</param>
/// <param name="nYSrc">源对象的左上角的X坐标</param>
/// <param name="dwRop">光栅的操作值,真彩含透明窗体请使用CopyPixelOperation.CaptureBlt | CopyPixelOperation.SourceCopy</param>
/// <returns></returns>
[DllImport("gdi32.dll")]
public static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, System.Drawing.CopyPixelOperation dwRop);
//建立屏幕Graphics
Graphics screenGraphic = Graphics.FromHwnd(IntPtr.Zero);
//根据屏幕大小建立位图,这就是最后截取到的*屏幕图像*
Bitmap screenBitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, screenGraphic);
//建立位图相关Graphics
Graphics bitmapGraphics = Graphics.FromImage(screenBitmap);
//建立屏幕上下文
IntPtr hdcScreen = screenGraphic.GetHdc();
//建立位图上下文
IntPtr hdcBitmap = bitmapGraphics.GetHdc();
//将屏幕捕获保存在位图中
Win32API.BitBlt(hdcBitmap, 0, 0, rectangle.Width, rectangle.Height, hdcScreen, rectangle.X, rectangle.Y, CopyPixelOperation.CaptureBlt | CopyPixelOperation.SourceCopy);
//关闭位图句柄
bitmapGraphics.ReleaseHdc(hdcBitmap);
//关闭屏幕句柄
screenGraphic.ReleaseHdc(hdcScreen);
//将截图尺寸规格化到指定尺寸
Bitmap reSizseBitmap = new Bitmap(rectangle.Width, rectangle.Height);
Graphics reSizeBitmapGraphics = Graphics.FromImage(reSizseBitmap);
reSizeBitmapGraphics.DrawImage(screenBitmap, new Rectangle(0, 0, rectangle.Width, rectangle.Height), new Rectangle(0, 0, rectangle.Width, rectangle.Height), GraphicsUnit.Pixel);
//为释放 Graphics 和由 FromImage 方法创建的相关资源,您应该始终调用 Dispose 方法。
reSizeBitmapGraphics.Dispose();
//释放位图对像
bitmapGraphics.Dispose();
//释放屏幕对像
screenGraphic.Dispose();
screenBitmap是全屏图像