2010-04-14 50 views
1

Silverlight的應用程序,我節省了位圖這樣的:爲什麼Silverlight代碼在從IsolatedStorage中讀取BitmapImage時會發生「災難性故障」?

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

閱讀它背出來是這樣的:

​​

但總是給我一個「災難性故障:HRESULT:0x8000FFFF(E_UNEXPECTED))「錯誤。

我已經看到了,當我試圖讀取png文件關閉這實際上是一個文本文件服務器之前這個錯誤,所以我假設位圖沒有被正確保存,我got the code here

任何人都可以看到如何正確保存BitmapImage?或者爲什麼它會給我這個錯誤?

更新:

當創建的BitmapImage,我看到的是被寫入的字節數組是1876年字節長,他們都爲0,爲什麼會是這樣?

回答

1

我得到它的工作,但我不清楚爲什麼。我這個代碼後調用SaveBitmapImageToIsolatedStorageFile(...)

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

如果我把它叫做之前的代碼,然後它的作品。

顯然SetSource()將e.Result中的字節清零,但保持長度?

+1

SetSource將從流中讀取(e.Result),所以流中的位置將會改變,但長度不會改變。我不確定是否允許您在結果流上查找,但是您可以使用MemoryStream作爲緩衝區並回到開始再次使用數據。 – toby 2011-07-08 15:27:28

相關問題