不再需要 BitmapImage 后如何释放内存? [英] How to free the memory after the BitmapImage is no longer needed?

查看:43
本文介绍了不再需要 BitmapImage 后如何释放内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我将 BitmapImage 加载到 Image 控件中,该控件位于 Window 上.然后我使用 Image 控件,然后关闭 Window.

First, I load a BitmapImage into the Image control, whice is located on the Window. Then 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 from the memory for some reason when the window is closed.

那么如何手动从 Image.Source 控件卸载 BitmapImage 以释放 RAM?

So how do I unload BitmapImage from Image.Source control manually to free the RAM?

推荐答案

我相信您正在寻找的解决方案位于 http://www.ridgesolutions.ie/index.php/2012/02/03/net-wpf-bitmapimage-file-locking/.就我而言,我试图找到一种在创建文件后删除文件的方法,但它似乎可以解决这两个问题.

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.

不释放内存:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆