0

I am a WP7 developer. I was using the following function:

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

to write a byteArray to a bitmap image. But in some cases it fires the exception as:

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)

I using the try..catch block to catch the exception but exception is not caught.

Alex Poole
  • 183,384
  • 11
  • 179
  • 318

1 Answers1

0

I made this function and doing this way

  private BitmapSource ConvertPic(Stream Image)
        {
            BitmapSource ContactImage;
            if (Image == null)
            {
                return ContactImage = null;
            }
            var bmp = new BitmapImage();
            bmp.SetSource(Image);
            ContactImage = bmp;
            return ContactImage;
        }
Arslan Pervaiz
  • 1,575
  • 4
  • 29
  • 62