In Previous discussion, we have seen the code Convert from System.Drawing.Icon to System.Media.ImageSource and vice versa
System.Windows.Drawing.Image is the base class of and System.Drawing.Bitmap, so that you can expect that the method for System.Drawing.Bitmap also applies for the System.Drawing.Image..
NOTE: the System.Drawing.Icon has no relationship with System.Drawing.Bitmap or System.Drawing.Image, there exist some conversion to Icon and vice versa, but don't expect the same approach can work with System.Drawing.Icon smoothly.
This introduce yet another way to convert from System.Drawing.Image to System.Windows.Media.ImageSource.
public static System.Windows.Media.ImageSource ConvertDrawingImage2MediaImageSource(System.Drawing.Image image)
{
using (var ms = new MemoryStream())
{
var bitmap = new System.Windows.Media.Imaging.BitmapImage();
bitmap.BeginInit();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
ms.Seek(0, System.IO.SeekOrigin.Begin);
bitmap.StreamSource = ms;
bitmap.EndInit();
return bitmap;
}
}
转换System.Drawing.Image到System.Windows.Media.ImageSource

本文介绍了一种将System.Drawing.Image转换为System.Windows.Media.ImageSource的方法。通过使用内存流和BitmapImage类,可以实现两种图像类型之间的转换。需要注意的是,此方法不适用于System.Drawing.Icon类型的转换。
896

被折叠的 条评论
为什么被折叠?



