Click here to Skip to main content
15,891,248 members
Articles / Silverlight

Binding to Image Path in Isolated Storage

Rate me:
Please Sign up or sign in to vote.
4.75/5 (3 votes)
19 Jun 2013CPOL 14.3K   4   1
Binding to Image Path in Isolated Storage

Binding Image Source to web address is not problem, it will work well... until you don't care about performance. The problem will appear if you want to cache images. Isolated storage is the only way to store downloaded files in Silverlight, no matter Browser or Windows Phone. It is not so difficult to download image file from the Internet and save to Isolated Storage, but how to use it in Silverlight Page?

Well, let's try to figure it out. First define the image converter:

C#
public class IsoImageConverter : IValueConverter
{
    //Convert Data to Image when Loading Data
    public object Convert(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        var bitmap = new BitmapImage();
        try
        {
            var path = (string)value;
            if (!String.IsNullOrEmpty(path))  
                {
                using (var file = LoadFile(path))
                {
                    bitmap.SetSource(file);
                }
            }
        }
        catch
        {
        }
        return bitmap;
    }

    private Stream LoadFile(string file)
    {
        using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
        {
            return isoStore.OpenFile(file, FileMode.Open, FileAccess.Read);
        }
    }
    
    public object ConvertBack(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Next, define the link in App.xaml for use in XAML code:

XML
<textarea style="display:none;" 
class="originalCode"><local:IsoImageConverter x:Key="IsoImageCoverter"/>
</textarea>

And finally bind to string path to Isolated Storage image file:

XML
Image Source="{Binding ImagePath, Converter=
{StaticResource IsoImageCoverter}}"/>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) i-BLADES
Thailand Thailand
I'm Android and Full Stack Software Engineer. 28 years in software industry, lots of finished projects. I’m never afraid of learning something new and you can see it by amount of skills in my resume.

I'm working remotely since 2009, self-motivated and self-organized.

There are no impossible projects. I have experience with Android, iOS, Web, Desktop, Embedded applications, VR, AR, XR, Computer vision, Neural networks, Games, IoT, you name it.

Comments and Discussions

 
Questiondownload file? Pin
mastone5-Sep-13 22:33
mastone5-Sep-13 22:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.