[转]WPF的BitmapImage的文件无法释放及内存泄露的问题

相信用过WPF的BitmapImage的,都在用类似这样的代码来解决文件无法删除的问题!

如果看看msdn上简单的描述,可以看到这样的说明:


如果 StreamSource 和 UriSource 均设置,则忽略 StreamSource 值。

如果要在创建 BitmapImage 后关闭流,请将 CacheOption 属性设置为 BitmapCacheOption.OnLoad。 默认 OnDemand 缓存选项保留对流的访问,直至需要位图并且垃圾回收器执行清理为止。

 

static class AppHelper
{
public static BitmapImage GetBitmapImage(string path)
{
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();

image.CacheOption = BitmapCacheOption.OnLoad;
bitmap.StreamSource = new MemoryStream(File.ReadAllBytes(path));
bitmap.EndInit();
bitmap.Freeze();
return bitmap;
}
}

 

文章来源 http://www.cnblogs.com/nio-nio/archive/2011/05/11/2043622.html#undefined

转载于:https://www.cnblogs.com/luohengstudy/p/4398330.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值