2012-06-18 43 views
0

我是WP7開發人員。我用的是以下功能:將源設置爲BitmapImage時出錯

public BitmapImage SetImageSource(byte[] byteArray) 
{ 
    BitmapImage bitmap = new BitmapImage();   
    try 
    { 
     MemoryStream ms = new MemoryStream(byteArray, 0, byteArray.Length); 
     bitmap.SetSource(ms); 
     ms.Flush(); 
     ms.Dispose(); 
    } 

    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
    return bitmap; 
} 

寫的ByteArray位圖圖像。 但在某些情況下,它觸發的例外:

System.OutOfMemoryException was unhandled 
Message=OutOfMemoryException 
StackTrace: 
     at MS.Internal.FrameworkCallbacks.NotifyManagedDebuggerOnNativeOOM() 
     at MS.Internal.XcpImports.BitmapSource_SetSourceNative(IntPtr bitmapSource, CValue& byteStream) 
     at MS.Internal.XcpImports.BitmapSource_SetSource(BitmapSource bitmapSource, CValue& byteStream) 
     at System.Windows.Media.Imaging.BitmapSource.SetSourceInternal(Stream streamSource) 
     at System.Windows.Media.Imaging.BitmapImage.SetSourceInternal(Stream streamSource) 
     at System.Windows.Media.Imaging.BitmapSource.SetSource(Stream streamSource) 
     at FileEncrypt.ListImage.SetImageSource(Byte[] byteArray) 
     at FileEncrypt.ListImage.LoadFiles() 
     at FileEncrypt.ListImage.ListImage_Loaded(Object sender, RoutedEventArgs e) 
     at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args) 
     at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName) 

我使用try..catch block捕獲異常,但異常沒有被捕獲。

+0

沒有例外出現在兩臺設備和仿真器上? –

+0

是的..當我第一次運行應用程序時,它確定但是當我連續導航到圖像列表的頁面時,發生異常 – user1463008

+0

您可以提供有關使用此功能的上下文的更多信息/代碼嗎? –

回答

0

我做了這個功能,做這樣

private BitmapSource ConvertPic(Stream Image) 
     { 
      BitmapSource ContactImage; 
      if (Image == null) 
      { 
       return ContactImage = null; 
      } 
      var bmp = new BitmapImage(); 
      bmp.SetSource(Image); 
      ContactImage = bmp; 
      return ContactImage; 
     } 
+0

異常仍然存在。 – user1463008

相關問題