///   将容器内容转成图片导出,这里的controller就是this
        /// </summary>
        private void OutTheControllerToPic()
        {
 
            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 = @"png|*.png|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;
                            case "png" :
                                imgformat = System.Drawing.Imaging. ImageFormat.Png;
                                break;
                            default:
                                MessageBox.Show("只能存取为: jpg,bmp,gif,png 格式" );
                                isSave = false;
                                break;
                        }
                    }
                    //默认保存为JPG格式  
                    if (imgformat == null )
                    {
                        imgformat = System.Drawing.Imaging. ImageFormat.Jpeg;
                    }
                    if (isSave)
                    {
                        try
                        {
                            bitmap.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;
        }

C# 将容器内容转成图片导出的更多相关文章

  1. 用JavaScript将Canvas内容转化成图片的方法

    上周我们花了半天时间开发下一个准备放进Mozilla Marketplace的应用.有一个应用现在非常的火热,那就是Instagram,Facebook花了100万美元收购了它.我们也想有100万美元 ...

  2. 个人永久性免费-Excel催化剂功能第54波-批量图片导出,调整大小等

    图片作为一种数据存在,较一般的存放在Excel单元格或其他形式存在的文本数据,对其管理更为不易,特别是仅有Excel原生的简单的插入图片功能时,Excel催化剂已全面覆盖图片数据的使用场景,无论是图片 ...

  3. 修改css的(屏蔽)overflow: hidden;实现浏览器能把网页全图保存成图片

    摘要: 1.项目需要,需要对网页内容“下载”保存成全图片 2.QQ浏览器等主流浏览器都支持这种下载保存功能 3.项目需要场景:编写好的项目维护文档,放在服务器上.如果是txt不能带图片可视化,如果wo ...

  4. Android:将View的内容映射成Bitmap转图片导出

    前段时间在网上看到这么个例子是将view映射到一个bitmap中,稍加改进可以用于一些截图工具或者截图软件(QQ截图之类),例子写的不够完善,不过很有些学习的意义内容大致如下: 在Android中自有 ...

  5. 同步手绘板——将View的内容映射成Bitmap转图片导出

    在Android中自有获取view中的cache内容,然后将内容转换成bitmap,方法名是:getDrawingCache(),返回结果为Bitmap,但是刚开始使用的时候,得到的结果都是null, ...

  6. Enterprise Architect 13 : 将绘制的图形导出成图片 或者 拷贝到剪贴板中

    使用Enterprise Architect 13 工具画类图.包图.需求图.状态图.时序图什么的导出成图片格式 或者拷贝到剪贴版,步骤如下: Publish -> Save Image -&g ...

  7. 前端借助dom-to-image把HTML转成图片并通过ajax上传到服务器

    之前接到了一个任务,把jsp中的table转成一个图片,保存在指定文件夹并显示在前端. 我的思路是:一.引用第三方js在前端把table转成图片 二.通过ajax把图片上传到服务器,保存在指定文件夹 ...

  8. 关于View转化成bitmap保存成图片

    产品今天说项目分享时要分享出一张  封面图片 + 几行文字 + 二维码图片 的图片. 思索了一下 封面图片和二维码图片让后台给接口得到地址, 主要还是找个方式得到一个包含这些内容的图片.于是就想能不能 ...

  9. OpenFlashChart的图片导出

    http://www.cnblogs.com/mcmurphy/archive/2012/10/15/2724894.html http://teethgrinder.co.uk/open-flash ...

随机推荐

  1. What's the difference between <b> and <strong>, <i> and <em> in HTML/XHTML? When should you use each?

    ref:http://stackoverflow.com/questions/271743/whats-the-difference-between-b-and-strong-i-and-em The ...

  2. python进阶(四)---需要了解的魔法方法

    以下内容,源于个人理解所得,纯属臆测,爱信不信:-D.欢迎大家留言讨论指正. 1.__new__魔法方法: 原型:__new__(cls, *args, **kwargs) 说明:__new__魔法方 ...

  3. 更改SQL Server 数据库的排序规则

    更改数据库的排序规则,SQL提示 5030 的错误,错误信息如下: The database could not be exclusively locked to perform the operat ...

  4. [原]Linux ssh远程连接断开问题处理办法

    我们在通过远程连接操作Linux server的时候,有可能过一段时间忘记操作,便会发生ssh断开的问题. 而如果是本地的server,比较好办,直连设备kill掉ssh,踢掉无效用户连接,再次链接即 ...

  5. 【转载】动态新增svg节点

    原文地址:http://blog.csdn.net/tomatomas/article/details/50442497 原文作者:番茄大圣 创建svg节点时,要使用createElementNS函数 ...

  6. django例子,question_text为中文时候报错

    问题描述 UnicodeEncodeError at /admin/polls/question/3/ 'ascii' codec can't encode characters in positio ...

  7. servlet获取表单数据的方式和编码方式

    .在servlet中获取表单的数据的几种方式  1>request.getParameter(“name”)://获取指定名称的值,返回值类型是一个字符串  2>request.getPa ...

  8. Win10无法安装提示磁盘布局不受UEFI固件支持怎样解决

    微软在推出Win10系统以后,就向Win7和Win8.1系统用户提供了免费升级Win10系统的推送,但是用户在安装Win10系统的时候,却有一部分用户反映,遇到提示“无法安装Windows,因为这台电 ...

  9. web应用动态文档技术

    动态生成web文档分为服务器动态生成.客户端动态生成 服务器动态生成文档技术主要有: CGI - 公共网关接口,它是一个允许Web服务器与后端程序以及脚本进行通信的标准化接口.通常是web服务器收到一 ...

  10. js根据ID修改背景图片

    <SCRIPT language=javascript> function expand(el) { var childObj = document.getElementById(&quo ...