Load a WPF BitmapImage from a System.Drawing.Bitmap

Kevin picture Kevin · Sep 18, 2008 · Viewed 223.4k times · Source

I have an instance of a System.Drawing.Bitmap and would like to make it available to my WPF app in the form of a System.Windows.Media.Imaging.BitmapImage.

What would be the best approach for this?

Answer

Pawel Lesnikowski picture Pawel Lesnikowski · Jul 1, 2009

How about loading it from MemoryStream?

using(MemoryStream memory = new MemoryStream())
{
    bitmap.Save(memory, ImageFormat.Png);
    memory.Position = 0;
    BitmapImage bitmapImage = new BitmapImage();
    bitmapImage.BeginInit();
    bitmapImage.StreamSource = memory;
    bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
    bitmapImage.EndInit();
}