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

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


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.


Uri src = new Uri(image_source, UriKind.RelativeOrAbsolute);var small_image_bmp = new BitmapImage();small_image_bmp.BeginInit();small_image_bmp.CacheOption = BitmapCacheOption.OnLoad;small_image_bmp.UriSource = src;small_image_bmp.EndInit();image_small_pic.Source = small_image_bmp;