BitmapImageのにInteropBitmap
-
19-09-2019 - |
質問
私は、WPFのImageコントロールでそれを使用できるようにBitmap (SystemIcons.Question)
にBitmapImage
を変換しようとしています。
私はBitmapSource
に変換するには、次の方法がありますが、それは今の問題はInteropBitmapImage
に変換する方法である、BitmapImage
を返します。直接的なキャストが動作するようには思えません。
誰もがそれを行う方法を知っていますか?
CODEます:
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を返すプロパティ:(私のイメージコントロールにバインド)
public BitmapImage QuestionIcon
{
get
{
return (BitmapImage)ConvertToBitmapSource();
}
}
解決
InteropBitmapImage
がImageSource
から継承するので、あなたはImage
制御に直接使用することができます。あなたはそれがBitmapImage
である必要はありません。
他のヒント
あなたは使用することができる必要があります:
public BitmapImage QuestionIcon
{
get
{
using (MemoryStream ms = new MemoryStream())
{
System.Drawing.Bitmap dImg = SystemIcons.ToBitmap();
dImg.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
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;
}
}
}
所属していません StackOverflow