try
{
//获取屏幕宽
int iWidth = Screen.PrimaryScreen.Bounds.Width;
//获取屏幕高
int iHeight = Screen.PrimaryScreen.Bounds.Height;
//按照屏幕宽高创建位图
Image img = new Bitmap(iWidth, iHeight);
//从一个继承自Image类的对象中创建Graphics对象
Graphics gc = Graphics.FromImage(img);
//抓屏并拷贝到myimage里
gc.CopyFromScreen(new System.Drawing.Point(0, 0), new System.Drawing.Point(0, 0), new Size(iWidth, iHeight));
//this.BackgroundImage = img;
//保存
string path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
System.Windows.Forms.SaveFileDialog dig_saveImage = new System.Windows.Forms.SaveFileDialog();
dig_saveImage.Title = "请选择图像保存路径";
dig_saveImage.Filter = "Image File(*.png)|Image File|*.tif|*.png|Image File(*.jpg)|*.jpg|Image File(*.bmp)|*.bmp";
dig_saveImage.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
dig_saveImage.FileName = DateTime.Now.ToString("yyyyMMddHHmmss");
if (dig_saveImage.ShowDialog() == DialogResult.OK)
img.Save(dig_saveImage.FileName); //保存位图
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
来源:CSDN
作者:YangTG1130
链接:https://blog.csdn.net/weixin_44713908/article/details/104653086