C#窗体截图保存的实现

来源:互联网 发布:ipad pro 记笔记 知乎 编辑:程序博客网 时间:2024/05/21 06:58

    在项目要求实现对窗体的部分控件进行截图,并保存。这里我整理了一下有关截图的过程。包括截图的范围(两种情况,第一种是对整个窗体截图;第二种是对窗体中的控件进行局部截图)、截图后保存的方式(两种情况,第一种是弹出对话框进行选择路径保存;第二种是默认路径直接保存)。

    说明1:代码中为了方便,我将截图的两种方法的代码独立分开来写的。

    说明2:文中对窗体中的控件截图(部分截图)指的是WebBrowser1。

    具体的过程会在代码的注释给出,不对的地方请大家多指点。

    首先是截图:用一个aboutBtn按钮来实现界面操作,其中aboutBtn_Click(objectsender, EventArgs e)包含两种方法(别忘了注释内容)实现如下:

        privatevoidaboutBtn_Click(object sender,EventArgse)

        {

            //控制截图的大小

            //Bitmapbit = new Bitmap(this.Width, this.Height);//截图本窗体大小

            Bitmap bit = new Bitmap(webBrowser1.Width, webBrowser1.Height);//针对具体情况进行调整

            Graphics g = Graphics.FromImage(bit);

            //g.CopyFromScreen(this.Location,new Point(0, 0), bit.Size);//对整个窗体进行截图

           g.CopyFromScreen(webBrowser1.PointToScreen(Point.Empty),Point.Empty, bit.Size);

            RealtimeVideoForm.image = bit;

 

            //对截图进行保存(保存方式为第二种:弹出对话框进行选择路径保存)(打开保存窗体时,存在bug现象)

            //saveBtn_Click(null,null);

 

            //对截图进行保存(保存方式为第一种:默认路径进行保存)(当天的生成文件夹)

           AutoSavaBtn_Click(null,null);

        }

    弹出对话框进行保存:

        //对截图进行保存(保存方式为第二种:弹出对话框进行选择路径保存)

        privatestaticvoidsaveBtn_Click(object sender, System.EventArgs e)

        {

            boolisSave = true;

            SaveFileDialog saveImageDialog = newSaveFileDialog();

           saveImageDialog.Title = "Capture screen image savedialog";

           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.ImageFormatimgformat =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

                        {

                            RealtimeVideoForm.image.Save(fileName, imgformat);

                            MessageBox.Show("图片成功保存!~~");

                        }

                        catch

                        {

                            MessageBox.Show("保存失败!");

                        }

                   }

               }

            }

        }

    默认路径自动保存:

        //对截图进行保存(保存方式为第一种:默认路径进行保存)(当天的生成单独文件夹)

        privatevoidAutoSavaBtn_Click(object sender,EventArgs e)

        {

            stringOpath = @"XXX\YYY\ZZZ";//保存的路径

            stringphotoname = DateTime.Now.Ticks.ToString();

            if(Opath.Substring(Opath.Length - 1, 1) != @"\")

               Opath = Opath + @"\";

            stringpath1 = Opath + DateTime.Now.ToShortDateString();

            if(!Directory.Exists(path1))

               Directory.CreateDirectory(path1);

            RealtimeVideoForm.image.Save(path1+"\\" + photoname + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

}

这就是整个截图功能。文档下载链接:http://download.csdn.net/detail/qq_30507287/9557893
0 0
原创粉丝点击