为什么这个 silverlight 代码会出现“灾难性失败”?从IsolatedStorage 中读取BitmapImage 时?
在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我让它工作,但不清楚到底为什么。我在这段代码之后调用SaveBitmapImageToIsolatedStorageFile(...):
如果我在该代码之前调用它,那么它就可以工作。
显然 SetSource() 将 e.Result 中的字节清零,但保留长度?
I got it to work but am unclear exactly why. I was calling SaveBitmapImageToIsolatedStorageFile(...) after this code:
If I call it before that code, then it works.
Apparently SetSource() zeroes out the bytes in e.Result but keeps the length?