InteropBitmap zu Bitmap
-
19-09-2019 - |
Frage
Ich versuche, eine Bitmap (SystemIcons.Question)
zu einem BitmapImage
zu konvertieren, damit ich es in einem WPF-Image-Steuerelement verwenden kann.
Ich habe folgendes Verfahren an einen BitmapSource
zu konvertieren, aber es gibt eine InteropBitmapImage
, jetzt das Problem ist, wie es zu einem BitmapImage
zu konvertieren. Eine direkte Besetzung scheint nicht zu arbeiten.
Weiß jemand, wie es zu tun?
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;
}
Eigenschaft Bitmap zurückzukehren: (gebunden an mein Image Control)
public BitmapImage QuestionIcon
{
get
{
return (BitmapImage)ConvertToBitmapSource();
}
}
Lösung
InteropBitmapImage
erbt von ImageSource
, so können Sie es direkt in einer Image
Steuerung verwenden. Sie brauchen nicht es ein BitmapImage
zu sein.
Andere Tipps
Sie sollten in der Lage sein zu verwenden:
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;
}
}
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow