Error in converting streams

1

I try to get an image stream from IsolatedStorage then assign it a bitmap image, like this:

using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(filePath, FileMode.Open, isolatedStorage))
{
    BitmapImage image = new BitmapImage();
    image.SetSource(fileStream);
    return image;
}

But in image.SetSource(fileStream) line, I get this error:

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

Update I removed using block, and still the error occurs just exactly when it reaches to that line. Maybe I write the file incorrectly at first place?. this is what I do for saving file:

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();
}
c#
windows-phone-7
windows-phone-8
windows-phone
isolatedstorage
asked on Stack Overflow Sep 8, 2013 by user2757047 • edited Sep 8, 2013 by user2757047

1 Answer

1

Your code to save the picture is wrong. You're writing the result of image.ToString(), not the actual image. As a rule of thumb, remember that StreamWriter is used to write strings into a stream. Never try to use it to write binary data.

using (var isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
    using (var fileStream = new IsolatedStorageFileStream(filePath, FileMode.Create, isolatedStorage))
    {
        var bitmap = new WriteableBitmap(image, null);
        bitmap.SaveJpeg(fileStream, bitmap.PixelWidth, bitmap.PixelHeight, 0, 100);
    }
}
answered on Stack Overflow Sep 8, 2013 by Kevin Gosse

User contributions licensed under CC BY-SA 3.0