Reloading an image in wpf

Bastiaan picture Bastiaan · Sep 29, 2009 · Viewed 11.9k times · Source

I'm trying to reload an image (System.Windows.Controls.Image) I display in WPF. I set the source like this:

ScreenAtco01Image.Source = new BitmapImage(new Uri(@"Y:/screenshots/naratco08-0-0-screenshot.png", UriKind.RelativeOrAbsolute));

I made a button, which should force a reload of this image (it changes on disk every second).

I have tried resetting the Source, but that doesn't do anything. However if I change the Source to a different image, this different image does get loaded. It seems like something is beeing cached?

Thanks for you help.

Answer

Bastiaan picture Bastiaan · Sep 29, 2009

Found an answer that works for me:

BitmapImage _image = new BitmapImage();
_image.BeginInit();
_image.CacheOption = BitmapCacheOption.None;
_image.UriCachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache);
_image.CacheOption = BitmapCacheOption.OnLoad;
_image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
_image.UriSource = new Uri(@"Y:/screenshots/naratco08-0-0-screenshot.png", UriKind.RelativeOrAbsolute);
_image.EndInit();
ScreenAtco01Image.Source = _image;