InteropBitmap to BitmapImage

后端 未结 3 1536
[愿得一人]
[愿得一人] 2021-01-18 21:13

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

相关标签:
3条回答
  • 2021-01-18 21:34

    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);
                    ms.Position = 0;
                    System.Windows.Media.Imaging.BitmapImage bImg = new System.Windows.Media.Imaging.BitmapImage();
                    bImg.BeginInit();
                    bImg.StreamSource = new MemoryStream(ms.ToArray());
                    bImg.EndInit();
                    return bImg;
                }
            }
        }
    
    0 讨论(0)
  • 2021-01-18 21:48

    InteropBitmapImage inherits from ImageSource, so you can use it directly in an Image control. You don't need it to be a BitmapImage.

    0 讨论(0)
  • 2021-01-18 21:54
    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 讨论(0)
提交回复
热议问题