【截图】WINFORM中几句程序获取整个屏幕的图片及当前窗口的图片快照

时间:2022-06-05 23:14:14

本文参考了【http://blog.csdn.net/johnsuna/article/details/8087483】的内容,自己测试后再上自己的部分理解和注释,仅供参考。

/// <summary>
/// 截取WinForm控件,本例截取窗体中的【panel1】中的内容;
/// </summary>
/// <returns>截取的图片</returns>
private Image GetWinformImage()
{
//定义Bitmap;
Bitmap bmp = new Bitmap(this.panel1.Width, this.panel1.Height);
//在指定范围内画出截取到的图片,范围宽度或高度不够则右侧或下侧会显示不完整;
this.panel1.DrawToBitmap(bmp, new Rectangle(0, 0, this.panel1.Width, this.panel1.Height));
//bmp.Save("D:\\Job\\T\\1.bmp");

return b;
}

/// <summary>
/// 截取屏幕范围内的内容;
/// </summary>
/// <returns>截取的图片</returns>
private Image GetScreenImage()
{
Image image = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics g = Graphics.FromImage(image); //将【image】与【Graphics】,可以进行在处理;
g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.PrimaryScreen.Bounds.Size); //截取;
g.DrawImage(image, 50, 50, 100, 100); //在指定范围内画出截取到的【整张】图片;
//image.Save("D:\\Job\\T\\2.bmp");
g.Dispose(); //释放【Graphics】不解释;

return image;
}