InteropBitmap for BitmapImage

I am trying to convert Bitmap (SystemIcons.Question) to BitmapImage , so I can use it in a WPF Image control.

I have the following method for converting it to BitmapSource , but it returns InteropBitmapImage , now the problem is how to convert it to BitmapImage . The direct order does not seem to work.

Does anyone know how to do this?

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; } 

to return BitmapImage: (tied to my image control)

 public BitmapImage QuestionIcon { get { return (BitmapImage)ConvertToBitmapSource(); } } 
+4
source share
3 answers

InteropBitmapImage inherits from ImageSource , so you can use it directly in the Image control. You do not need this to be a BitmapImage .

+7
source

You should be able to use:

  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; } } } 
+1
source
 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; } } } 
0
source

Source: https://habr.com/ru/post/1304038/


All Articles