Conversion of various image formats

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.