WPF Refreshing Images (Cache Problem)
I have a big problem with loading an image dynamically in my applikation. When i start the applikation the placeholder image source is empty. when i click on a button an image is created and loaded as the placeholder source. when i click again a new image is created but the old image is shown. The creation of the image works perfect. The files on my disk are the f开发者_如何学JAVAiles they should be.
The following function is setting the source of the image placeholder.
public void setImage(string path)
{
BitmapImage img = new BitmapImage();
img.BeginInit();
img.UriSource = new Uri(path, UriKind.Relative);
img.EndInit();
//Set Refreshing Options
img.CacheOption = BitmapCacheOption.None;
img.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
placeholder.Source = img;
}
The two options BitmapCacheOption and the BitmapCreateOptions dont change anything.
Can anybody of you help me?
WPF caches images internally, for performance reasons. If you're using the same Uri both times, and expecting to get a different image each time (for example, if the Uri is on a Web server that returns a random image), then this cache will be a problem for you.
You'll probably have to create a WebRequest and manually download the image, rather than relying on the Image class to do it for you.
Another option would be to change the Uri in a trivial way that makes it unique. For example, you could append a GUID as a query string.
精彩评论