Skip to main content

Caching Images downloaded from web on Windows Phone Isolated storage



I was helping on a Windows Phone application where the requirement was to cache the images the phone downloads on the isolated storage for offline viewing.
I wanted a solution which was simple and as transparent as possible. While researching I found  someone wrote a Silverlight converter for loading images from isolated storage. Taking that as a base I created a converted which can
  1. Load image from web (http + https), and persist it to isolated storage.
  2. In case of network connectivity issues can load the same image from isolated storage. It does that by mapping the http url to a isolated storage location.
  3. In case the network is down and the image is neither there in cache, loads a default image, passed as parameter to converter.
Here is the gist for the implementation.
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.IO.IsolatedStorage;
using System.IO;
using System.Windows.Media.Imaging;
namespace Util.Converters
{
/// <summary>
/// Caches the image that gets downloaded as part of Image control Source property.
/// </summary>
public class CacheImageFileConverter : IValueConverter
{
private static IsolatedStorageFile _storage = IsolatedStorageFile.GetUserStoreForApplication();
private const string imageStorageFolder = "TempImages";
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string path = value as string;
if (String.IsNullOrEmpty(path)) return null;
Uri imageFileUri = new Uri(path);
if (imageFileUri.Scheme == "http" || imageFileUri.Scheme == "https")
{
if (!Phone.GetIsNetworkAvailable())
{
if (_storage.FileExists(GetFileNameInIsolatedStorage(imageFileUri)))
{
return ExtractFromLocalStorage(imageFileUri);
}
else
{
return LoadDefaultIfPassed(imageFileUri, (parameter ?? string.Empty).ToString());
}
}
else
{
return DownloadFromWeb(imageFileUri);
}
}
else
{
BitmapImage bm = new BitmapImage(imageFileUri);
return bm;
}
}
private static object LoadDefaultIfPassed(Uri imageFileUri, string defaultImagePath)
{
string defaultImageUri = (defaultImagePath ?? string.Empty).ToString();
if (!string.IsNullOrEmpty(defaultImageUri))
{
BitmapImage bm = new BitmapImage(new Uri(defaultImageUri,UriKind.Relative)); //Load default Image
return bm;
}
else
{
BitmapImage bm = new BitmapImage(imageFileUri);
return bm;
}
}
private static object DownloadFromWeb(Uri imageFileUri)
{
WebClient m_webClient = new WebClient(); //Load from internet
BitmapImage bm = new BitmapImage();
m_webClient.OpenReadCompleted += (o, e) =>
{
if (e.Error != null || e.Cancelled) return;
WriteToIsolatedStorage(IsolatedStorageFile.GetUserStoreForApplication(), e.Result, GetFileNameInIsolatedStorage(imageFileUri));
bm.SetSource(e.Result);
e.Result.Close();
};
m_webClient.OpenReadAsync(imageFileUri);
return bm;
}
private static object ExtractFromLocalStorage(Uri imageFileUri)
{
string isolatedStoragePath = GetFileNameInIsolatedStorage(imageFileUri); //Load from local storage
using (var sourceFile = _storage.OpenFile(isolatedStoragePath, FileMode.Open, FileAccess.Read))
{
BitmapImage bm = new BitmapImage();
bm.SetSource(sourceFile);
return bm;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
private static void WriteToIsolatedStorage(IsolatedStorageFile storage, System.IO.Stream inputStream, string fileName)
{
IsolatedStorageFileStream outputStream = null;
try
{
if (!storage.DirectoryExists(imageStorageFolder))
{
storage.CreateDirectory(imageStorageFolder);
}
if (storage.FileExists(fileName))
{
storage.DeleteFile(fileName);
}
outputStream = storage.CreateFile(fileName);
byte[] buffer = new byte[32768];
int read;
while ((read = inputStream.Read(buffer, 0, buffer.Length)) > 0)
{
outputStream.Write(buffer, 0, read);
}
outputStream.Close();
}
catch
{
//We cannot do anything here.
if (outputStream != null) outputStream.Close();
}
}
/// <summary>
/// Gets the file name in isolated storage for the Uri specified. This name should be used to search in the isolated storage.
/// </summary>
/// <param name="uri">The URI.</param>
/// <returns></returns>
public static string GetFileNameInIsolatedStorage(Uri uri)
{
return imageStorageFolder + "\\" + uri.AbsoluteUri.GetHashCode() + ".img";
}
}
}


To use the converter
  1. Import the name space.
  2. Declare the converter as resource.
  3. Set the Image Source Property to use this converter like this 

Popular posts from this blog

IIS Url Rewrite and HTTP POST data

If you play around with IIS Url Rewriting rules and try to do redirects on an HTTP POST you loose your POST data. To make sure you get the post data you have to set the `redirectType` to `Temporary` within your rules. So the action configuration looks like this <action redirectType=" Temporary " type="Redirect" url="http://{HTTP_HOST}{REQUEST_URI}"> </action> You may think what scenario warrant a POST request redirect. We faced one such scenario while doing SSO with a federated Identity Provider (IP)  such as Google, Azure AD. In federated authentication scenario once the user is authenticated by the IP, it redirects back to your site with claim tokens in a POST request over secure channel (https). In our case we wanted to redirect to user back http after receiving the request. But any redirects were causing loss of token. By doing a 307 (Temporary) redirect we were able to preserve the post data.

Integrating ASP.Net MVC with AngularJS

We recently released a Project Template for ASP.Net MVC and AngularJS here and here . I did a follow up post detailing how the project template has been setup and how to make AngularJS and MVC play well together on my company blog . If you are interested in understanding how the template works head over to my blog post and read it!