为什么这个 silverlight 代码会出现“灾难性失败”?从IsolatedStorage 中读取BitmapImage 时?

发布于 2024-08-28 16:32:07 字数 1935 浏览 10 评论 0原文

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。为什么会这样呢?

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?

Update:

When the BitmapImage is created, I see that the array of bytes that gets written is 1876 bytes long and they are all 0. Why would that be?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木緿 2024-09-04 16:32:07

我让它工作,但不清楚到底为什么。我在这段代码之后调用SaveBitmapImageToIsolatedStorageFile(...):

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

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

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

I got it to work but am unclear exactly why. I was calling SaveBitmapImageToIsolatedStorageFile(...) after this code:

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

If I call it before that code, then it works.

Apparently SetSource() zeroes out the bytes in e.Result but keeps the length?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文