26

I´ve got an instance of System.Drawing.Image.

How can I show this in my WPF-application?

I tried with img.Source but that does not work.

user896692
  • 2,351
  • 7
  • 36
  • 57
  • Possibly Related: http://stackoverflow.com/questions/1118496/using-image-control-in-wpf-to-display-system-drawing-bitmap – Alain Apr 09 '12 at 18:27
  • 3
    Possible duplicate of [wpf - Can i use System.Drawing in wpf?](http://stackoverflow.com/questions/10663056/wpf-can-i-use-system-drawing-in-wpf) – The_Black_Smurf Sep 07 '16 at 11:37

3 Answers3

26

I have the same problem and solve it by combining several answers.

System.Drawing.Bitmap bmp;
Image image;
...
using (var ms = new MemoryStream())
{
    bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
    ms.Position = 0;

    var bi = new BitmapImage();
    bi.BeginInit();
    bi.CacheOption = BitmapCacheOption.OnLoad;
    bi.StreamSource = ms;
    bi.EndInit();
}

image.Source = bi;
//bmp.Dispose(); //if bmp is not used further. Thanks @Peter

From this question and answers

Community
  • 1
  • 1
Badiboy
  • 1,519
  • 1
  • 18
  • 31
21

To load an Image into a WPF Image control you will need a System.Windows.Media.ImageSource.

You need to convert your Drawing.Image object to an ImageSource object :

 public static BitmapSource GetImageStream(Image myImage)
    {
        var bitmap = new Bitmap(myImage);
        IntPtr bmpPt = bitmap.GetHbitmap();
        BitmapSource bitmapSource =
         System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
               bmpPt,
               IntPtr.Zero,
               Int32Rect.Empty,
               BitmapSizeOptions.FromEmptyOptions());

        //freeze bitmapSource and clear memory to avoid memory leaks
        bitmapSource.Freeze();
        DeleteObject(bmpPt);

        return bitmapSource;
    }

Declaration of the DeleteObject method.

[DllImport("gdi32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool DeleteObject(IntPtr value);
AlexDrenea
  • 7,981
  • 1
  • 32
  • 49
14

If you use a converter, you can actually bind to the Image object. You will just need to create an IValueConverter that will convert the Image to a BitmapSource.

I used AlexDrenea's sample code inside the converter to do the real work.

[ValueConversion(typeof(Image), typeof(BitmapSource))]
public class ImageToBitmapSourceConverter : IValueConverter
{
    [DllImport("gdi32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    internal static extern bool DeleteObject(IntPtr value);

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is null || !(value is Image myImage))
        {//ensure provided value is valid image.
            return null;
        }

        if (myImage.Height > Int16.MaxValue || myImage.Width > Int16.MaxValue)
        {//GetHbitmap will fail if either dimension is larger than max short value.
            //Throwing here to reduce cpu and resource usage when error can be detected early.
            throw new ArgumentException($"Cannot convert System.Drawing.Image with either dimension greater than {Int16.MaxValue} to BitmapImage.\nProvided image's dimensions: {myImage.Width}x{myImage.Height}", nameof(value));
        }

        using Bitmap bitmap = new Bitmap(myImage); //ensure Bitmap is disposed of after usefulness is fulfilled.
        IntPtr bmpPt = bitmap.GetHbitmap();
        try
        {
            BitmapSource bitmapSource =
             System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                   bmpPt,
                   IntPtr.Zero,
                   Int32Rect.Empty,
                   BitmapSizeOptions.FromEmptyOptions());

            //freeze bitmapSource and clear memory to avoid memory leaks
            bitmapSource.Freeze();
            return bitmapSource;
        }
        finally
        { //done in a finally block to ensure this memory is not leaked regardless of exceptions.
            DeleteObject(bmpPt);
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

In your XAML you will need to add the converter.

<utils:ImageToBitmapSourceConverter x:Key="ImageConverter"/>

<Image Source="{Binding ThumbSmall, Converter={StaticResource ImageConverter}}"
                   Stretch="None"/>
IAmJersh
  • 742
  • 8
  • 25
burnttoast11
  • 1,164
  • 16
  • 33