zoukankan      html  css  js  c++  java
  • WPF BitmapImage 占用资源无法释放、无法删除问题

    使用Image控件显示图片后,虽然自己释放了图片资源,Image.Source =null 了一下,但是图片实际没有释放。
    解决方案:修改加载方式~
            public static BitmapImage GetImage(string imagePath)
            {
                BitmapImage bitmap = new BitmapImage();
                if (File.Exists(imagePath))
                {
                    bitmap.BeginInit();
                    bitmap.CacheOption = BitmapCacheOption.OnLoad;
                    using (Stream ms = new MemoryStream(File.ReadAllBytes(imagePath)))
                    {
                        bitmap.StreamSource = ms;
                        bitmap.EndInit();
                        bitmap.Freeze();
                    }
                }
                return bitmap;
            }
      //使用时直接通过调用此方法获得Image后立马释放掉资源
           ImageBrush berriesBrush = new ImageBrush();   
           berriesBrush.ImageSource = GetImage(path); //path为图片的路径        
           this.Background = berriesBrush;

  • 相关阅读:
    LeetCode 338. 比特位计数
    LeetCode 208. 实现 Trie (前缀树)
    初识restful api接口
    破解 Navicat Premium 12
    ES6 Reflect的认识
    ES6 WeakMap和WeakSet的使用场景
    sublime 注释模版插件DocBlockr的使用
    js call方法的使用
    ES6 Generator的应用场景
    ES6 Symbol的应用场景
  • 原文地址:https://www.cnblogs.com/qiantao/p/9413811.html
Copyright © 2011-2022 走看看