Show Drawing.Image in WPF

后端 未结 3 894
我寻月下人不归
我寻月下人不归 2020-11-29 08:24

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.

3条回答
  •  孤独总比滥情好
    2020-11-29 08:52

    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.

    
    
    
    

提交回复
热议问题