Here's the
ValueConverter
I wrote years ago.
It converts a
Bitmap
to
ImageSource
(specifically a
BitmapFrame
):
using System;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace ResourcesLibrary
{
[ValueConversion(typeof(Bitmap), typeof(ImageSource))]
public class BitmapToImageSourceConverter : IValueConverter
{
[DllImport("gdi32.dll")]
private static extern bool DeleteObject(IntPtr hObject);
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Bitmap bmp = value as Bitmap;
if (bmp == null)
return null;
using (Stream str = new MemoryStream())
{
bmp.Save(str, System.Drawing.Imaging.ImageFormat.Bmp);
str.Seek(0, SeekOrigin.Begin);
BitmapDecoder bdc = new BmpBitmapDecoder(str, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
return bdc.Frames[0];
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}