Tuesday, October 16, 2012

Caching images in WinRT

WinRT will out-of-the-box cache all the images you download for your. The cache will automatically be used when you set the image source to a URL:
e.Source=http://somesource/aRandomImage.jpg
If 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:
“?Cache=” + System.DateTime.Now.DayOfYear.ToString() + System.DateTime.Now.Hour.ToString();

The BitmapImage class also has some properties that allow us to control the caching behavior:

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:
Member nameDescription
DefaultCaches the entire image into memory. This is the default value.
OnDemandCreates a memory store for requested data only. The first request loads the image directly; subsequent requests are filled from the cache.
OnLoadCaches the entire image into memory at load time. All requests for image data are filled from the memory store.
NoneDo not create a memory store. All requests for the image are filled directly by the image file.

4 comments:

Richard C. Lambert said...

WinRT will out-of-the-box cache all the images you download for your. The cache will automatically be used when you set the image source to a URL: music painting

Robert F. Crocker said...

I am however grant from you, despite I am difficult to obtain my desires. I truly maintain session numerous that is published on your blog. plagiarism detect

SEO Brothers said...

Truly excellent proportion man . I love it and i’m going to share it . I am telling each readers of this blog have to share this post . Anyways, that’s a great article. Plagiarism now-days is a hot topic and we all need to be careful with our words, especially in public speeches. If anyone need to check their speech for plagiarism use – Plagiarism Detector .

Honey Wilson said...

This guide is so helpful for me! I hope you will continue to share such useful posts here. online check for plagiarism