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;

  • 相关阅读:
    模块
    Queue(队列)
    Stack(栈)
    Vector(容器)
    位图像素的颜色
    大数处理之三(除法)
    大数处理之二(幂运算)
    浮点数(double)的优势
    大数处理之一(加法和乘法)
    Depth-First Search
  • 原文地址:https://www.cnblogs.com/qiantao/p/9413811.html
Copyright © 2011-2022 走看看