zoukankan      html  css  js  c++  java
  • WPF的BitmapImage的文件无法释放及内存泄露的问题

    相信用过WPF的BitmapImage的,都在用类似这样的代码来解决文件无法删除的问题!

    如果看看msdn上简单的描述,可以看到这样的说明:


    如果 StreamSource 和 UriSource 均设置,则忽略 StreamSource 值。

    如果要在创建 BitmapImage 后关闭流,请将 CacheOption 属性设置为 BitmapCacheOption.OnLoad。 默认 OnDemand 缓存选项保留对流的访问,直至需要位图并且垃圾回收器执行清理为止。

    static class AppHelper
    {
    public static BitmapImage GetBitmapImage(string path)
    {
    BitmapImage bitmap = new BitmapImage();
    bitmap.BeginInit();

    image.CacheOption = BitmapCacheOption.OnLoad;
    bitmap.StreamSource = new MemoryStream(File.ReadAllBytes(path));
    bitmap.EndInit();
    bitmap.Freeze();
    return bitmap;
    }
    }

  • 相关阅读:
    iptables
    iftop
    sed&awk
    rz&sz
    关于springboot + mybatis plus 使用事务
    关于JsonArray.toList转换
    jmeter脚本录制
    去掉百度右边的百度热搜等干扰项,集中注意力呀~~
    报错
    图片的异步上传
  • 原文地址:https://www.cnblogs.com/luluping/p/2603047.html
Copyright © 2011-2022 走看看