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) { }
        }
    }
    
  • 相关阅读:
    Java中的引用
    JVM参数调优
    GCRoots
    JVM体系结构
    死锁编码及定位分析
    线程池(Java中有哪些方法获取多线程)
    Synchronized和Lock的区别
    阻塞队列BlockingQueue
    CountDownLatch/CyclicBarrier/Semaphore
    浅谈二分
  • 原文地址:https://www.cnblogs.com/MichaelLoveSna/p/14497164.html
Copyright © 2011-2022 走看看