e.Source=http://somesource/aRandomImage.jpgIf you set the same URL again then it will automatically use the cached image if it has already been downloaded once.
But what if you don’t want this behavior? Ian Walkers blog brings us the answer:
He has found that adding a dummy querystring to the BitMapImage source filename reference does the trick and also can offer precise control over the length of caching required .
For example, if you want to cache your images for an hour, add the following to the image path:
The BitmapImage class also has some properties that allow us to control the caching behavior:“?Cache=” + System.DateTime.Now.DayOfYear.ToString() + System.DateTime.Now.Hour.ToString();
BitmapImage bi = new BitmapImage(); // Begin initialization. bi.BeginInit(); // Set properties. bi.CacheOption = BitmapCacheOption.OnDemand; bi.CreateOptions = BitmapCreateOptions.IgnoreImageCache; // End initialization. bi.EndInit();
When IgnoreImageCache is selected, any existing entries in the image cache are replaced even if they share the same Uri.
The BitmapCacheOption offers you the following options:
|Default||Caches the entire image into memory. This is the default value.|
|OnDemand||Creates a memory store for requested data only. The first request loads the image directly; subsequent requests are filled from the cache.|
|OnLoad||Caches the entire image into memory at load time. All requests for image data are filled from the memory store.|
|None||Do not create a memory store. All requests for the image are filled directly by the image file.|