WPF应用程序,释放资源(图像) [英] WPF application, freeing resources (images)

查看:307
本文介绍了WPF应用程序,释放资源(图像)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个幻灯片演示应用程序,可以显示一些图像.我有一个显示图像的简单用户控件.我从XMl文件读取图像,并且每次在新的usercontrol上创建新实例并将其显示在我的应用程序中.在某个时候,我需要刷新"图像(例如新版本),然后从应用程序中删除所有用户控件,然后将它们清空",然后尝试删除光盘上的所有图像.但是,这为我提供了一个例外,即资源"myimage.png"正在被另一个进程使用,并且无法删除.我该怎么做才能释放"我的资源,以便可以删除它们?

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?

欢呼

推荐答案

我已经使用.NET Refector查看了WPF源代码,并发现

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.

这篇关于WPF应用程序,释放资源(图像)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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