Reputation: 1383
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.
Upvotes: 1
Views: 4614
Reputation: 1383
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.
Upvotes: 0
Reputation: 37060
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 Binding
s update the control in response to the raising of INotifyPropertyChanged.PropertyChanged
and INotifyCollectionChanged.CollectionChanged
events in your viewmodel.
Upvotes: 1