Load a WPF BitmapImage from a System.Drawing.Bitmap

前端 未结 10 2169
天涯浪人
天涯浪人 2020-11-22 04:39

I have an instance of a System.Drawing.Bitmap and would like to make it available to my WPF app in the form of a System.Windows.Media.Imaging.BitmapImage<

10条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 05:32

    I came to this question because I was trying to do the same, but in my case the Bitmap is from a resource/file. I found the best solution is as described in the following link:

    http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage.aspx

    // Create the image element.
    Image simpleImage = new Image();    
    simpleImage.Width = 200;
    simpleImage.Margin = new Thickness(5);
    
    // Create source.
    BitmapImage bi = new BitmapImage();
    // BitmapImage.UriSource must be in a BeginInit/EndInit block.
    bi.BeginInit();
    bi.UriSource = new Uri(@"/sampleImages/cherries_larger.jpg",UriKind.RelativeOrAbsolute);
    bi.EndInit();
    // Set the image source.
    simpleImage.Source = bi;
    

提交回复
热议问题