问题描述
我正在尝试将 Bitmap(SystemIcons.Question)
转换为 BitmapImage
,以便可以在WPF Image控件中使用它.
I'm trying to Convert a Bitmap (SystemIcons.Question)
to a BitmapImage
so I can use it in a WPF Image control.
我有以下方法将其转换为 BitmapSource
,但它返回 InteropBitmapImage
,现在的问题是如何将其转换为 BitmapImage
.直接投射似乎无效.
I have the following method to convert it to a BitmapSource
, but it returns an InteropBitmapImage
, now the problem is how to convert it to a BitmapImage
. A direct cast does not seem to work.
有人知道怎么做吗?
代码:
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;
}
返回BitmapImage的属性:(绑定到我的图像控件)
property to return BitmapImage: (Bound to my Image Control)
public BitmapImage QuestionIcon
{
get
{
return (BitmapImage)ConvertToBitmapSource();
}
}
推荐答案
InteropBitmapImage
继承自 ImageSource
,因此您可以直接在 Image 代码>控件.您不需要将其作为
BitmapImage
.
InteropBitmapImage
inherits from ImageSource
, so you can use it directly in an Image
control. You don't need it to be a BitmapImage
.
这篇关于将InteropBitmap转换为BitmapImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!