c# winform实现截图并保持图片功能

news/2024/7/5 5:04:21 标签: c#

      最近项目需要对界面进行截图并保存的功能,在网上搜索了下结合实际需求最终完成功能,代码如下

   //调用导图

   private void pictureBox4_Click(object sender, EventArgs e)
        {

            Bitmap bitmap = new Bitmap(this.Width, this.Height);
            DrawToBitmap(this, bitmap, new Rectangle(0, 0, this.Width, this.Height));

            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
                        {
                            images.Save(fileName, imgformat);
                            //MessageBox.Show("图片已经成功保存!");   
                        }
                        catch
                        {
                            MessageBox.Show("保存失败,你还没有截取过图片或已经清空图片!");
                        }
                    }
                }
            }

        }

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SendMessage(HandleRef hWnd, int msg, IntPtr wParam, IntPtr lParam);

        [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
        public static extern bool BitBlt(HandleRef hDC, int x, int y, int nWidth, int nHeight, HandleRef hSrcDC, int xSrc, int ySrc, int dwRop);

        /// <summary>
        /// 支持呈现到指定的位图。
        /// </summary>
        public static Bitmap DrawToBitmap(Control control, Bitmap bitmap, Rectangle targetBounds)
        {
            if (bitmap == null)
            {
                throw new ArgumentNullException("bitmap");
            }
            if (((targetBounds.Width <= 0) || (targetBounds.Height <= 0)) || ((targetBounds.X < 0) || (targetBounds.Y < 0)))
            {
                throw new ArgumentException("targetBounds");
            }
            Bitmap image = new Bitmap(control.Width, control.Height, bitmap.PixelFormat);
            using (Graphics graphics = Graphics.FromImage(image))
            {
                IntPtr hdc = graphics.GetHdc();
                SendMessage(new HandleRef(control, control.Handle), 0x317, hdc, (IntPtr)30);
                using (Graphics graphics2 = Graphics.FromImage(bitmap))
                {
                    IntPtr handle = graphics2.GetHdc();
                    BitBlt(new HandleRef(graphics2, handle), 0, 0, control.Width, control.Height, new HandleRef(graphics, hdc), targetBounds.X, targetBounds.Y,

0xcc0020);
                    graphics2.ReleaseHdcInternal(handle);
                }
                graphics.ReleaseHdcInternal(hdc);
            }
            return image;
        }



本文转自夜&枫博客园博客,原文链接:http://www.cnblogs.com/newstart/archive/2012/08/29/2662474.html,如需转载请自行联系原作者


http://www.niftyadmin.cn/n/1117771.html

相关文章

python中idle什么意思_python中idle是什么意思

python中idle是什么意思&#xff1f;IDLE是Python软件包自带的一个集成开发环境&#xff0c;初学者可以利用它方便地创建、运行、测试和调试Python程序。IDLE是开发 python 程序的基本IDE(集成开发环境)&#xff0c;具备基本的IDE的功能&#xff0c;是非商业Python开发的不错的…

课堂讨论

我们的小组是唐炳辉、蒋陵郡、孙哲、王宏伟。我们讨论的结果是这样的&#xff1a; 唐炳辉说&#xff1a;我们应该确定系统的业务范围&#xff0c;数据的来源和去向。而且&#xff0c;我们要动态跟踪用户的要求。 蒋陵郡说&#xff1a;利益相关者主要有系统管理员&#xff08;教…

Valentine's Day Round 1001.Ferries Wheel(hdu 5174)解题报告

题目链接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid5174 题目意思&#xff1a;给出 n 个人坐的缆车值&#xff0c;假设有 k 个缆车&#xff0c;缆车值 A[i] 需要满足&#xff1a;A[i−1]<A[i]<A[i1](1<i<K)。现在要求的是&#xff0c;有多少人满足&a…

mysql+php

1.Mysql安装 * yum install gcc-c ncurses-devel -y ##解决依赖性 * tar zxf mysql-boost-5.7.17.tar.gz ##解压文件 * tar zxf mysql-boost-5.7.17.tar.gz ##安装CMAKE工具 * useradd -M -d /usr/local/lnmp/nginx -s /sbin/nologin -u 800 nginx ##添加用户 * cm…

jQuery ajax的traditional参数的作用

为什么80%的码农都做不了架构师&#xff1f;>>> 一般的&#xff0c;可能有些人在一个参数有多个值的情况下&#xff0c;可能以某个字符分隔的形式传递&#xff0c;比如页面上有多个checkbox&#xff1a; $.ajax{url:"xxxx",data:{p: "123,456,789&…

关于target=_new和_blank的区别!

为了弄清楚这个问题我们来看三段代码产生的结果&#xff1a; code1&#xff1a; <html><head><title> new和blank的区别 </title></head><frameset cols"30%, *">     <!-- 分别调用target为new和blank的两段代码 --&g…

windows安装mysql初始密码_Windows下安装mysql 8+ 及修改初始密码

1、解压压缩包&#xff0c;放置到任意目录注意目录最好是英文这是我的目录&#xff1a;D:\Python\mysql-8.0.12-winx64\2. 初始化用管理员权限打开CMD或者Windows Powershell使用命令&#xff1a;mysqld --initialize --console使用CMD命令操作如下&#xff1a;C:\Windows\syst…

linux 查看Linux本机IP

2019独角兽企业重金招聘Python工程师标准>>> 1.命令&#xff1a;ifconfig 2.命令&#xff1a;ip addr 参考链接&#xff1a;https://blog.csdn.net/u014346344/article/details/81126144 转载于:https://my.oschina.net/qimhkaiyuan/blog/3007317