1

Assume I have an Image object which his Source property being initialized in the following way:

BitmapImage source = new BitmapImage();
source.BeginInit();
source.CacheOption = BitmapCacheOption.OnLoad;
source.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
source.UriSource = new Uri("C:\\Temp\\tech\\window.jpg");
source.EndInit();
image.Source = source; // image object automatically initialized in the window construction.

As you can implicitly see, window.jpg is an image that permanently changed (get deleted and replaced with a new different image with the same name).

I have been using the above mentioned way to initialize the Image object Source property because I have found it at Image does not refresh in custom picture box as a way to support the Image refreshing.

However, when deleting and replacing window.jpg while the program is running, the Image object still shows the image that was loaded when we initialized its Source property (cache).

It seems like the Image object ignores the BitmapCreateOptions.IgnoreImageCache value and doesn't refresh the image when a new one replacing the previous that was deleted.

Any solution will be welcomed.

EDIT

I don't want to change the filename because I would need to redundantly find a random name that doesn't exist instead of just refreshing the image to load the same Source again.

I don't want to change the Source property value because of the fact that I don't change the filename.

The desired solution is to update the Image object each time the image file changes. The image change controlled by me so there is no need to bind and wait for a change, instead, I would force an image refresh, if it possible.

Community
  • 1
  • 1
Eido95
  • 1,313
  • 1
  • 15
  • 29
  • Any solution to what? The only question I see is you asking to reload the `Image` without changing the value of the `Source` property. I don't think that's possible unless you use a writeable bitmap object (e.g. `WritableBitmap`, `RenderTargetBitmap`, etc.). But what's wrong with changing the `Source` property? And are you finding that when you change the `Source` property your image still does not get updated? Please provide a good [mcve] that reliably reproduces your problem, along with a precise description of what that code does and what you want it to do instead. – Peter Duniho May 19 '16 at 18:47
  • @PeterDuniho, thank you for your replay. Please check my edit. – Eido95 May 23 '16 at 19:31

2 Answers2

1

I would have an Image in XAML, and in the XAML, I would bind Image.Source to a property of type ImageSource on my viewmodel. I'd have a FileSystemWatcher update that ImageSource property when the .jpg on the disk changed, and I would raise PropertyChanged when that happened.

In XAML, you update UI control content/properties by using the Binding class to bind viewmodel properties to the control's dependency properties. The Bindings update the control in response to the raising of INotifyPropertyChanged.PropertyChanged and INotifyCollectionChanged.CollectionChanged events in your viewmodel.

0

My current solution is whenever I delete and create a new window.jpg, I set my Image.Source property with the Image.Source property initialization mentioned in the question.

Eido95
  • 1,313
  • 1
  • 15
  • 29