zoukankan      html  css  js  c++  java
  • WPF 基础

    1. 功能

    系统截图。

    2. 实现

    2.1 思路
    1. 控件继承自 System.Windows.Media.Visual,
    2. 通过 System.Windows.Media.Imaging.RenderVisualToBitmap 把 Visual 对象转换为位图 rtb
    3. 将位图转成编码器接受的一帧,类型为 BitmapFrame :BitmapFrame.Create(rtb)
    4. 将 BitmapFrame 添加到编码器 : encode.Frames.Add(BitmapFrame.Create(rtb))
    5. 将编码器的数据输出到文件 encode.Save(fs)
    2.2 代码
    class ImageHelper
    {
        public static RenderTargetBitmap RenderVisualToBitmap(Visual visual, int width, int height)
        {
            var rtb = new RenderTargetBitmap(width, height, 96d, 96d, PixelFormats.Default);
            rtb.Render(visual);
    
            return rtb;
        }    
    }
    
    class ControlImage
    {
        public void Get(Visual visual, int width, int height, string PicType)
        {
            // 1.获取 visual 的控件内容,保存为位图
            RenderTargetBitmap rtb = Common.RenderVisualToBitmap(visual, width, height);
            
            try
            {
                string encodeClassName = "System.Windows.Media.Imaging." + PicType + "BitmapEncoder";
                
                // 2.实例化一个图片编码器
                BitmapEncoder encode =
                (BitmapEncoder)Assembly.LoadFile(Environment.CurrentDirectory + "/PresentationCore.dll").CreateInstance(encodeClassName); //需要复制 dll 到本地
                
                // 3. 把控件的位图转成编码器接受的图像数据。
                encode.Frames.Add(BitmapFrame.Create(rtb));
    
                //新建文件
                using (Stream fs = File.Create(path))
                {
                    //将位图编码为指定的流
                    encode.Save(fs);
                }
            }
            catch (Exception e) { }
        }
    }
    
  • 相关阅读:
    巴基斯坦:软件服务外包行业的后来者 (zz)
    对象集合查询
    我的db类库 新版
    得到web.config里配置项的数据库连接字符串
    jdk环境变量配置
    FastReport v3.2.5在BDS2006中的安装方法
    CONFIG.SYS文件的命令与配置
    DOS下内存的配置
    动态注册ODBC数据源的通用方法
    XP下安装装SQL2000企业版本
  • 原文地址:https://www.cnblogs.com/MichaelLoveSna/p/14497164.html
Copyright © 2011-2022 走看看