image图片解除占用 wpf,WPF应用程序,释放资源(图像)

在WPF应用程序中,遇到无法删除正在使用的图片资源问题。通过设置BitmapImage的CacheOption为BitmapCacheOption.OnLoad,可以将图片数据加载到内存并立即关闭文件,从而在需要时安全删除文件。这种方法避免了因文件被占用而引发的删除异常。
摘要由CSDN通过智能技术生成

i have a slideshow application showing some images. I have a simple usercontrol that displays an image. I read the images from an XMl file and each time i create a new instance on a new usercontrol and show it in my application. At some point i need to "refresh" theimages (e.g. new version) i remove all my usercontrols from the application and "null" them - then i try to delete all images on disc. However, this gives me an exception that the resource "myimage.png" is in use by another process and cannot be deleted. What do i have to do to "release" my resources so that i can delete them?

cheers,

解决方案

I've looked at the WPF source code using .NET Refector and found this answer to a related question that might be a better solution for you. Set the BitmapImage.CacheOption to BitmapCacheOption.OnLoad forces the image data into memory and closes the file immediately.

Using that option, you can delete the from the file system at any time afterwards.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值