1. Convert image to system. Windows. Controls. Image in Windows form
/// <Summary>
/// Convert drawingimage to the iamge class used by controls
/// </Summary>
/// <Param name = "gdiimg"> </param>
/// <Returns> </returns>
Private system. Windows. Controls. Image convertdrawingimagetowpfimage (system. Drawing. Image gdiimg)
{
System. Windows. Controls. Image IMG = new system. Windows. Controls. Image ();
// Convert system. Drawing. Image to WPF Image
System. Drawing. bitmap BMP = new system. Drawing. Bitmap (gdiimg );
Intptr hbitmap = BMP. gethbitmap ();
System. Windows. Media. imagesource wpfbitmap = system. Windows. InterOP. imaging. createbitmapsourcefromhbitmap (hbitmap, intptr. Zero, int32rect. Empty, bitmapsizeoptions. fromemptyoptions ());
IMG. Source = wpfbitmap;
IMG. width = 500;
IMG. Height = 600;
IMG. Stretch = system. Windows. Media. Stretch. Fill;
Return IMG;
}
2. Convert the image to system. Windows. Media. imagesource, which is the image source used in WPF.
/// <Summary>
/// Convert drawingimage to the iamge class in the class used in system. Windows. Media. imagesource WPF
/// </Summary>
/// <Param name = "gdiimg"> </param>
/// <Returns> </returns>
Private system. Windows. Media. imagesource convertdrawingimagetowpfimage (system. Drawing. Image gdiimg)
{
System. Windows. Controls. Image IMG = new system. Windows. Controls. Image ();
// Convert system. Drawing. Image to WPF Image
System. Drawing. bitmap BMP = new system. Drawing. Bitmap (gdiimg );
Intptr hbitmap = BMP. gethbitmap ();
Return System. Windows. InterOP. imaging. createbitmapsourcefromhbitmap (hbitmap, intptr. Zero, int32rect. Empty, bitmapsizeoptions. fromemptyoptions ());
}
Conversion of various image formats