Creating WPF BitmapImage

I have a ushort [] containing image data that I need to display on the screen the moment I create the Windows.System.Drawing.Bitmap file and convert it to BitmapImage, but it seems slow, this.

Anyone what is the fastest way to create a BitmapImage from ushort [] ??

or alternatively create an ImageSource object from data?

Thank,

Eamonn

+3
source share
1 answer

My previous method of converting Bitmap to BitmapImage:

MemoryStream ms = new MemoryStream(); 
bitmap.Save(ms, ImageFormat.Png); 
ms.Position = 0; 
BitmapImage bi = new BitmapImage(); 
bi.BeginInit(); 
bi.StreamSource = ms; 
bi.EndInit();

I was able to speed it up using

Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), 
                                      IntPtr.Zero, 
                                      Int32Rect.Empty,
                                      System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

EDIT: , , , bitmap.GetHbitmap , , , .net , , :

    [System.Runtime.InteropServices.DllImport("gdi32.dll")]
    public static extern bool DeleteObject(IntPtr hObject);

    IntPtr hBitmap = bitmap.GetHbitmap();
    try
    {
        imageSource = Imaging.CreateBitmapSourceFromHBitmap(hBitmap,
                                                            IntPtr.Zero,
                                                            Int32Rect.Empty,
                                                            System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
    }
    catch (Exception e) { }
    finally
    {
        DeleteObject(hBitmap);
    }

( DLL, msdn, , , - http://msdn.microsoft.com/en-us/library/1dz311e4.aspx)

+5

All Articles