Why does this silverlight code get a "catastrophic failure" when reading a BitmapImage out of Isolat

Posted by Edward Tanguay on Stack Overflow See other posts from Stack Overflow or by Edward Tanguay
Published on 2010-04-14T14:20:02Z Indexed on 2010/04/14 14:23 UTC
Read the original article Hit count: 778

In a Silverlight app, I save a Bitmap like this:

public static void SaveBitmapImageToIsolatedStorageFile(OpenReadCompletedEventArgs e, string fileName)
{
    using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(fileName, FileMode.Create, isf))
        {
            Int64 imgLen = (Int64)e.Result.Length;
            byte[] b = new byte[imgLen];
            e.Result.Read(b, 0, b.Length);
            isfs.Write(b, 0, b.Length);
            isfs.Flush();
            isfs.Close();
            isf.Dispose();
        }
    }
}

and read it back out like this:

public static BitmapImage LoadBitmapImageFromIsolatedStorageFile(string fileName)
{
    string text = String.Empty;

    using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
    {
        if (!isf.FileExists(fileName))
            return null;

        using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (IsolatedStorageFileStream isoStream = isoStore.OpenFile(fileName, FileMode.Open))
            {
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.SetSource(isoStream);
                return bitmapImage; // "Catastrophic Failure: HRESULT: 0x8000FFFF (E_UNEXPECTED))"
            }
        }
    }
}

but this always gives me a "Catastrophic Failure: HRESULT: 0x8000FFFF (E_UNEXPECTED))" error.

I've seen this error before when I tried to read a png file* off a server which was actually a **text file, so I assume the Bitmap is not being saved correctly, I got the code here.

Can anyone see how the BitmapImage is not being saved correctly? Or why it would be giving me this error?

© Stack Overflow or respective owner

Related posts about Silverlight

Related posts about isolatedstorage