interopbitmap إلى bitmapimage.
-
19-09-2019 - |
سؤال
أحاول تحويل Bitmap (SystemIcons.Question)
إلى أ BitmapImage
لذلك يمكنني استخدامه في عنصر تحكم صورة WPF.
لدي الطريقة التالية لتحويلها إلى 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;
}
الممتلكات لإعادة 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