Winform开发遇到的问题之截屏功能

1.代码块:

        //定义图片
        public static Bitmap image { get; set; }
        /// <summary>
        /// 截图按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnShot_Click(object sender, EventArgs e)
        {
            //控制截图大小
            Bitmap bit = new Bitmap(this.Width, this.Height);
            Graphics g = Graphics.FromImage(bit);
            //对整个窗体进行截图
            g.CopyFromScreen(this.Location, new Point(0, 0), bit.Size);
            AreaShow.image = bit;
            btnSavePic_Click(null, null);
        }

        /// <summary>
        /// 截图保存操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSavePic_Click(object sender, EventArgs e)
        {
            bool isSave = true;
            SaveFileDialog saveImageDialog = new SaveFileDialog();
            saveImageDialog.Title = "保存图片!";
            saveImageDialog.Filter = @"jpeg|*.jpg|bmp|*.bmp|gif|*.gif";
            if (saveImageDialog.ShowDialog() == DialogResult.OK)
            {
                string fileName = saveImageDialog.FileName.ToString();

                if (fileName != "" && fileName != null)
                {
                    string fileExtName = fileName.Substring(fileName.LastIndexOf(".") + 1).ToString();
                    System.Drawing.Imaging.ImageFormat imgformat = null;

                    if (fileExtName != "")
                    {
                        switch (fileExtName)
                        {
                            case "jpg":
                                imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
                                break;
                            case "bmp":
                                imgformat = System.Drawing.Imaging.ImageFormat.Bmp;
                                break;
                            case "gif":
                                imgformat = System.Drawing.Imaging.ImageFormat.Gif;
                                break;
                            default:
                                MessageBox.Show("只能存取为: jpg,bmp,gif 格式");
                                isSave = false;
                                break;
                        }
                    }
                    //默认保存为JPG格式
                    if (imgformat == null)
                    {
                        imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
                    }
                    if (isSave)
                    {
                        try
                        {
                            AreaShow.image.Save(fileName, imgformat);
                            MessageBox.Show("图片成功保存!~~");
                        }
                        catch
                        {
                            MessageBox.Show("保存失败!");
                        }
                    }
                }
            }

        }

2.效果图:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_31453627/article/details/82017985