How to store and get images in IsolatedStorage?

1

I want to be able to save images in IsolatedStorage, and later get it.

for this purpose I wrote a helper which I want to access it from everywhere in my app:

for creating an image:

public static void SaveImage(Stream image, string name)
{
    try
    {
        IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();

        if (!isolatedStorage.DirectoryExists("MyImages"))
        {
            isolatedStorage.CreateDirectory("MyImages");
        }

        var filePath = Path.Combine("MyImages", name + ".jpg");

        using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(filePath, FileMode.Create, isolatedStorage))
        {
            StreamWriter sw = new StreamWriter(fileStream);
            sw.Write(image);
            sw.Close();
        }
    }
    catch (Exception ex)
    {
        throw new Exception("failed");
    }
}

and for getting that image again:

public static BitmapImage Get(string name)
{
    try
    {
        IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();

        var filePath = Path.Combine("MyImages", name + ".jpg");

        using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(filePath, FileMode.Open, isolatedStorage))
        {
            BitmapImage image = new BitmapImage();
            image.SetSource(fileStream);
            return image;
        }
    }
    catch (Exception ex)
    {
        throw new Exception("failed");
    }
}

The problem appears when I try to set source of image I get this error message:

The component cannot be found. (Exception from HRESULT: 0x88982F50)

c#
windows-phone-7
windows-phone-8
windows-phone
isolatedstorage
asked on Stack Overflow Sep 7, 2013 by user2757047 • edited Sep 8, 2013 by user2757047

1 Answer

3

Assuming you have the right Capabilities under your WMAppManifest.xml what you need is:

public static void SaveImage(Stream image, string name)
{

    var bitmap = new BitmapImage();
    bitmap.SetSource(attachmentStream);
    var wb = new WriteableBitmap(bitmap);
    var temp = new MemoryStream();
    wb.SaveJpeg(temp, wb.PixelWidth, wb.PixelHeight, 0, 50);

    using (var myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
    {
        if (!myIsolatedStorage.DirectoryExists("MyImages"))
        {
            myIsolatedStorage.CreateDirectory("MyImages");
        }

        var filePath = Path.Combine("MyImages", name + ".jpg");

        using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(filePath, FileMode.Create, myIsolatedStorage))
        {
            fileStream.Write(((MemoryStream)temp).ToArray(), 0, ((MemoryStream)temp).ToArray().Length);
            fileStream.Close();
        }
    }
}

That should work and store your image as a jpeg into your desired directory. if it doesn't, make sure that the create directory and Path.Combine() methods are being used correctly.

answered on Stack Overflow Sep 9, 2013 by George Nikolaides

User contributions licensed under CC BY-SA 3.0