Displaying images in a list box keeps files locked
I have a list box of images which I am adding programatically from my my pictures directory. I want to be able to put images in the directory and the开发者_StackOverflow中文版 UI displays it automatically. I also want to be able to remove photos from the directory and have the UI update as well but the problem is that the images are being used by the application so I am not able to remove the images from the directory. Is there a way to unlock the files so I can rename and remove files from the directory?
Are you using data binding like Image Source = {Binding FilePath} ? If so, you need to load the image with CacheOption = BitmapCacheOption.OnLoad, you can do it declaratively with a ValueConverter, like this one:
http://soulsolutions.com.au/wpf-reference-an-image-without-a-file-lock/
Do you load images with Image.FromFile? I've noticed this behavior a long time ago. Try opening the files with File.Open, loading the images with Image.FromStream, then close the stream.
精彩评论