点击截图按钮后,把截图文件保存到指定的本地文件夹中:
按钮代码如下:
private void button1_Click(object sender, EventArgs e)
{
//Screen类表示单个系统上的一个或多个显示设备,PrimaryScreen属性是获取主显示;
Screen scr = Screen.PrimaryScreen;
//Rectangle存储一组整数,共四个,表示一个矩形的位置和大小。
Rectangle rc = scr.Bounds; //Bounds属性是获取显示的边界;
int iWidth = rc.Width;
int iHeight = rc.Height;
Image image = new Bitmap(iWidth, iHeight); //创建一个和屏幕同样大小的图像;
//Graphics是封装一个 GDI+ 绘图图面;
Graphics graph = Graphics.FromImage(image); //从指定的 Graphics 创建新的 Image,绘制这个图像;
//从屏幕到 Graphics 的绘图图面的位块传输。
graph.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(iWidth, iHeight));
string exePath = Directory.GetCurrentDirectory();
string ImagePath = @exePath + "\\ErrorScreenImage";
// 判断文件夹是否存在,如果文件平不存在则创建新的文件夹
if (!System.IO.Directory.Exists(@ImagePath))
{
System.IO.Directory.CreateDirectory(@ImagePath);//不存在就创建文件夹
}
image.Save(@ImagePath + "\\Error截屏 " + DateTime.Now.ToString("yyyy-MM-dd HHmmss") + ".jpg");
graph.Dispose(); //释放Graphics资源;
image.Dispose();
}