How to clear image control in WPF (C #)

I have an image control with the original image located on my c drive. I get a message that the image is being used by another process when I try to delete the original image in order to change it using another dynamically. How to remove an image from an image control to remove it.

I tried the following options:

string path = ((BitmapImage)img.Source).UriSource.LocalPath; img.SetValue(System.Windows.Controls.Image.SourceProperty, null); File.Delete(path); 

and

 string path = ((BitmapImage)img.Source).UriSource.LocalPath; img.Source = null; File.Delete(path) 

But it does not work ...

+4
source share
2 answers

Try customizing the bitmap through the stream source property. Thus, the application will not block the file, since you downloaded it through the stream.

http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage.streamsource(VS.85).aspx

+1
source

// this function allows you to load an image from a file and release it

  BitmapImage loadPhoto(string path) { BitmapImage bmi = new BitmapImage(); bmi.BeginInit(); bmi.CacheOption = BitmapCacheOption.OnLoad; bmi.CreateOptions = BitmapCreateOptions.IgnoreImageCache; bmi.UriSource = new Uri(path); bmi.EndInit(); return bmi; } 
0
source

Source: https://habr.com/ru/post/1306794/


All Articles