C# 截取屏幕

不羁的心 提交于 2020-03-05 06:35:39
 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());
            }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!