InteropBitmap到BitmapImage

Ton*_*ion 5 c# wpf bitmapsource bitmapimage interopbitmapimage

我正在尝试将a转换Bitmap (SystemIcons.Question)为a,BitmapImage因此我可以在WPF图像控件中使用它.

我有以下方法将其转换为a BitmapSource,但它返回一个InteropBitmapImage,现在的问题是如何将其转换为BitmapImage.直接演员似乎不起作用.

有谁知道怎么做?

码:

 public BitmapSource ConvertToBitmapSource()
        {
            int width = SystemIcons.Question.Width;
            int height = SystemIcons.Question.Height;
            object a = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(SystemIcons.Question.ToBitmap().GetHbitmap(), IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(width, height));

            return (BitmapSource)a;
        }
Run Code Online (Sandbox Code Playgroud)

返回BitmapImage的属性:(绑定到我的图像控件)

public BitmapImage QuestionIcon
        {
            get
            {
                return  (BitmapImage)ConvertToBitmapSource();
            }
        }
Run Code Online (Sandbox Code Playgroud)

Tho*_*que 8

InteropBitmapImage继承自ImageSource,因此您可以直接在Image控件中使用它.你不需要它是一个BitmapImage.