使用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;

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-02-06
  • 2021-09-26
  • 2021-05-23
  • 2022-12-23
  • 2022-01-08
  • 2021-07-30
猜你喜欢
  • 2021-09-14
  • 2022-12-23
  • 2022-12-23
  • 2022-01-01
  • 2021-12-14
  • 2021-06-09
相关资源
相似解决方案