不再需要BitmapImage后如何释放内存? [英] How to free the memory after the BitmapImage is no longer needed?
问题描述
首先,我在窗口的
。 Image
控件上加载 BitmapImage
其次,我使用 Image
控件,然后关闭窗口
。
First, I load a BitmapImage
on the Image
control on the Window
.
Second, I work with the Image
control and then close the Window
.
我在一分钟内完成了2-3次,而且我的记忆很快就会填满,因为当窗口关闭时图像不会卸载。
I do it 2-3 times in a minute and my memory fills up very quickly because the images do not unload back when the window is closed.
那么如何从 Image.Source
卸载 BitmapImage
手动控制以释放内存?
So how to unload BitmapImage
from Image.Source
control manually to free the memory?
推荐答案
我相信您正在寻找的解决方案是 http://www.ridgesolutions.ie/index.php/2012/02/03/net-wpf-bitmapimage-file锁/ 。就我而言,我试图在创建文件后找到一种删除文件的方法,但它似乎是两个问题的解决方案。
I believe the solution you are looking for is at http://www.ridgesolutions.ie/index.php/2012/02/03/net-wpf-bitmapimage-file-locking/. In my case, I was trying to find a way to delete the file after it was created, but it appears to be a solution to both issues.
不免费up memory:
Doesn't free up memory:
var bitmap = new BitmapImage(new Uri(imageFilePath));
释放内存,并允许删除文件:
Frees up memory, and allows file to be deleted:
var bitmap = new BitmapImage();
var stream = File.OpenRead(imageFilePath);
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.StreamSource = stream;
bitmap.EndInit();
stream.Close();
stream.Dispose();
也可以冻结BitmapImage:
Optionally, also freeze the BitmapImage:
bitmap.Freeze();
这篇关于不再需要BitmapImage后如何释放内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!