zoukankan      html  css  js  c++  java
  • WPF杂难解 粘贴板复制GIF图片时丢失透明背景

    问题来源:

    还是在上文文本编辑器中,用到了一个开源项目做公式编辑,即输入公式返回一张公式图片,此图片为GIF格式,透明背景。

    上文的方案解决了往RichTextBox内粘贴图文信息的功能,测试中发现复制公式的GIF图片,粘贴至任意位置都会变黑,仔细观察后发现原因为其透明背景丢失。

    相同的问题可见:

    http://stackoverflow.com/questions/998655/how-can-i-get-an-image-out-of-the-clipboard-without-losing-the-alpha-channel-in/

    http://stackoverflow.com/questions/6769534/black-image-when-saving-png-from-clipboard/

    解决方案:

    与上文同思路,在粘贴时对图片进行一次转换,代码如下:

    Bitmap bitmap = new Bitmap(strImagePath);//实例化一个新的BitMap

    System.Windows.Controls.Image img = new System.Windows.Controls.Image();
    //转换为BitmapSource 
    BitmapSource bmpSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
    //作为png的一帧 
    PngBitmapEncoder png = new PngBitmapEncoder();
    png.Frames.Add(BitmapFrame.Create(bmpSource));
    string id = DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
    string newPath = Constants.SYSTEM.TEMP_DATA_PATH + "\\" + id + ".png";
    //保存至本地 
    using (Stream stream = File.Create(newPath))
    {
        png.Save(stream);
    }

     这样做给图片加了一个白色的背景,在当前项目是可用的。但是估计不能通用,且效率同样不高,还是等高手指点。

  • 相关阅读:
    How to Integrate JCaptcha in Spring Security
    精简的webservice
    linux时间与Windows时间不一致的解决
    java泛型
    spring全局变量引起的并发问题
    ByteBuffer常用方法详解
    cindy
    NIO之Buffer的clear()、rewind()、flip()方法的区别
    Java NIO(New I/O)的三个属性position、limit、capacity
    技术选型
  • 原文地址:https://www.cnblogs.com/shen6041/p/2388154.html
Copyright © 2011-2022 走看看