c# - Convert from System.Drawing.Image to System.WIndows.Media.ImageSource
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.
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;
}
}