Show Drawing.Image in WPF

后端 未结 3 882
我寻月下人不归
我寻月下人不归 2020-11-29 08:24

I´ve got an instance of System.Drawing.Image.

How can I show this in my WPF-application?

I tried with img.Source but that does not work.

3条回答
  •  春和景丽
    2020-11-29 08:55

    I have the same problem and solve it by combining several answers.

    System.Drawing.Bitmap bmp;
    Image image;
    ...
    using (var ms = new MemoryStream())
    {
        bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        ms.Position = 0;
    
        var bi = new BitmapImage();
        bi.BeginInit();
        bi.CacheOption = BitmapCacheOption.OnLoad;
        bi.StreamSource = ms;
        bi.EndInit();
    }
    
    image.Source = bi;
    //bmp.Dispose(); //if bmp is not used further. Thanks @Peter
    

    From this question and answers

提交回复
热议问题