Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - WPF how to unreference a BitmapImage so I can delete the source file?

Tags:

c#

image

wpf

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

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;

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?

like image 902
cost Avatar asked Aug 17 '11 14:08

cost


Video Answer


1 Answers

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.

like image 116
dlev Avatar answered Sep 30 '22 15:09

dlev