1

我尝试从中获取图像流,IsolatedStorage然后为其分配位图图像,如下所示:

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

image.SetSource(fileStream)实际上,我收到了这个错误:

找不到该组件。(来自 HRESULT 的异常:0x88982F50)

更新我使用块删除,但仍然恰好在到达该行时发生错误。也许我一开始就写错了文件?这是我为保存文件所做的:

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();
}
4

1 回答 1

1

您保存图片的代码是错误的。您正在编写 的结果image.ToString(),而不是实际图像。根据经验,请记住StreamWriter用于将字符串写入流中。永远不要试图用它来写二进制数据。

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);
    }
}
于 2013-09-08T12:07:02.057 回答