C# - WPF如何unreference一个BitmapImage的,所以我可以删除源文件? [英] C# - WPF how to unreference a BitmapImage so I can delete the source file?

查看:417
本文介绍了C# - WPF如何unreference一个BitmapImage的,所以我可以删除源文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个相当简单的问题,但我似乎无法找出一种方法来解决它。

This seems like a fairly simple issue, but I can't seem to figure a way to work around it.

在一个WPF窗口,我有一个形象, image_small_pic。在相关的C#文件我设置使用此代码的值:

In a WPF window I have an image, image_small_pic. In the associated C# file I set the value of that using this code:

Uri src = new Uri(image_source, UriKind.RelativeOrAbsolute);
small_image_bmp = new BitmapImage(src);
image_small_pic.Source = small_image_bmp;



在哪里small_image_bmp是一个公共的BitmapImage对象。不过,如果那么,以后,如果我改变small_image_bmp到另一个文件并重新分配image_small_pic.Source,那么原来的图像仍然锁着,我不能删除它。即使我稍后尝试它仍然锁定。任何想法我怎么能释放这个吗?

Where small_image_bmp is a public BitmapImage object. But then if then, later on, if I change small_image_bmp to another file and reassign image_small_pic.Source, then the original image is still locked and I can't delete it. Even if I try later it's still locked. Any thoughts how I can free this up?

推荐答案

检查出的this文章。有与你对面过来的图像WPF一些奇怪的行为。解决的办法是在读取字节自己,然后创建基于这些图像,因为如果你让框架处理它,该文件将保持锁定状态。

Check out this article. There's some odd behaviour with WPF images that you're coming across. The solution is to read in the bytes yourself and then create an image based on them, since if you let the framework handle it, the file will remain locked.

这篇关于C# - WPF如何unreference一个BitmapImage的,所以我可以删除源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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