InteropBitmap to BitmapImage

Posted by Tony on Stack Overflow See other posts from Stack Overflow or by Tony
Published on 2010-03-13T22:21:44Z Indexed on 2010/03/13 22:25 UTC
Read the original article Hit count: 1360

Hi,

I'm trying to Convert a Bitmap (SystemIcons.Question) to a BitmapImage so I can use it in a WPF Image control.

I have the following method to convert it to a BitmapSource, but it returns an InteropBitmapImage, now the problem is how to convert it to a BitmapImage. A direct cast does not seem to work.

Does anybody know how to do it?

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

property to return BitmapImage: (Bound to my Image Control)

public BitmapImage QuestionIcon
        {
            get
            {
                return  (BitmapImage)ConvertToBitmapSource();
            }
        }

© Stack Overflow or respective owner

Related posts about wpf

Related posts about bitmapimage