C# InteropBitmap到BitmapImage
我正在尝试将C# InteropBitmap到BitmapImage,c#,wpf,bitmapimage,bitmapsource,interopbitmapimage,C#,Wpf,Bitmapimage,Bitmapsource,Interopbitmapimage,我正在尝试将位图(SystemIcons.Question)转换为位图图像,以便在WPF图像控件中使用它 我使用以下方法将其转换为BitmapSource,但它返回InteropBitmapImage,现在的问题是如何将其转换为BitmapImage。直接演员似乎不起作用 有人知道怎么做吗 代码: 属性返回BitmapImage:(绑定到我的图像控件) 您应该能够使用: public BitmapImage QuestionIcon { get {
位图(SystemIcons.Question)
转换为位图图像
,以便在WPF图像控件中使用它
我使用以下方法将其转换为BitmapSource
,但它返回InteropBitmapImage
,现在的问题是如何将其转换为BitmapImage
。直接演员似乎不起作用
有人知道怎么做吗
代码:
属性返回BitmapImage:(绑定到我的图像控件)
您应该能够使用:
public BitmapImage QuestionIcon
{
get
{
using (MemoryStream ms = new MemoryStream())
{
System.Drawing.Bitmap dImg = SystemIcons.ToBitmap();
dImg.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
ms.Position = 0;
System.Windows.Media.Imaging.BitmapImage bImg = new System.Windows.Media.Imaging.BitmapImage();
bImg.BeginInit();
bImg.StreamSource = new MemoryStream(ms.ToArray());
bImg.EndInit();
return bImg;
}
}
}
InteropBitmapImage
继承自ImageSource
,因此您可以在图像
控件中直接使用它。您不需要它是从ImageSource继承的BitmapImage
InteropBitmapImage,因此可以在图像控件中使用它。你为什么需要它成为位图图像?@Thomas,没错!我不需要做转换就解决了它!:)把你的评论作为回答,我会接受的!
public System.Windows.Media.Imaging.BitmapImage QuestionIcon
{
get
{
using (MemoryStream ms = new MemoryStream())
{
System.Drawing.Bitmap dImg = SystemIcons.ToBitmap();
dImg.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
ms.Position = 0;
var bImg = new System.Windows.Media.Imaging.BitmapImage();
bImg.BeginInit();
bImg.StreamSource = ms;
bImg.EndInit();
return bImg;
}
}
}
public BitmapImage QuestionIcon
{
get
{
using (MemoryStream ms = new MemoryStream())
{
System.Drawing.Bitmap dImg = SystemIcons.ToBitmap();
dImg.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
ms.Position = 0;
System.Windows.Media.Imaging.BitmapImage bImg = new System.Windows.Media.Imaging.BitmapImage();
bImg.BeginInit();
bImg.StreamSource = new MemoryStream(ms.ToArray());
bImg.EndInit();
return bImg;
}
}
}
public System.Windows.Media.Imaging.BitmapImage QuestionIcon
{
get
{
using (MemoryStream ms = new MemoryStream())
{
System.Drawing.Bitmap dImg = SystemIcons.ToBitmap();
dImg.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
ms.Position = 0;
var bImg = new System.Windows.Media.Imaging.BitmapImage();
bImg.BeginInit();
bImg.StreamSource = ms;
bImg.EndInit();
return bImg;
}
}
}