【问题标题】:Error while Setting source to BitmapImage将源设置为 BitmapImage 时出错
【发布时间】:2012-06-18 07:32:53
【问题描述】:

我是一名 WP7 开发人员。我正在使用以下功能:

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 捕获异常,但未捕获异常。

【问题讨论】:

  • 设备和模拟器上是否都出现了异常?
  • 是的..当我第一次运行应用程序时它没问题,但是当我连续导航到图像列表页面时,发生异常
  • 您能否提供更多有关使用此功能的上下文的信息/代码?

标签: c# windows-phone-7 memorystream bitmapimage


【解决方案1】:

我做了这个功能并这样做

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-10
    • 1970-01-01
    • 1970-01-01
    • 2015-02-13
    • 1970-01-01
    • 2016-05-23
    相关资源
    最近更新 更多