1

Silverlight应用程序中,我保存这样的位图

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

并像这样回来:

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))"
            }
        }
    }
}

但这总是给我一个“灾难性故障:HRESULT:0x8000FFFF (E_UNEXPECTED)) ”错误。

当我尝试从服务器读取实际上是文本文件的png 文件之前,我已经看到了这个错误,所以我认为位图没有正确保存,我在这里得到了代码

谁能看到 BitmapImage 没有正确保存?或者为什么它会给我这个错误?

更新:

创建 BitmapImage 时,我看到写入的字节数组是 1876 字节长,它们都是 0。为什么会这样?

4

1 回答 1

1

我让它工作,但不清楚具体原因。我在这段代码之后调用了 SaveBitmapImageToIsolatedStorageFile(...) :

BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(e.Result);
Image image = new Image();
image.Source = bitmapImage;

如果我在该代码之前调用它,那么它可以工作。

显然 SetSource() 将e.Result中的字节清零但保持长度?

于 2010-04-14T14:58:04.643 回答