Conversion of ImageSource and bitmap in WPF image control

Source: Internet
Author: User

1. Convert from bitmap to ImageSource

  
[DllImport ("Gdi32.dll", SetLastError =true)]        Private Static extern BOOLDeleteObject (IntPtr hobject); /// <summary>        ///convert from bitmap to ImageSource/// </summary>        /// <param name= "icon" ></param>        /// <returns></returns>         Public Staticimagesource Changebitmaptoimagesource (Bitmap Bitmap) {//Bitmap Bitmap = icon. Tobitmap ();IntPtr Hbitmap =bitmap.            Gethbitmap (); ImageSource Wpfbitmap=System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap (Hbitmap, IntPtr.Zero,            Int32rect.empty, Bitmapsizeoptions.fromemptyoptions ()); if(!DeleteObject (HBITMAP)) {                Throw NewSystem.ComponentModel.Win32Exception (); }            returnWpfbitmap; }

2. Convert from bitmap to BitmapSource//<summary>

        ///convert from bitmap to BitmapSource/// </summary>        /// <param name= "BMP" ></param>        /// <returns></returns>         Public StaticBitmapSource Changebitmaptobitmapsource ( ThisBitmap BMP)            {BitmapSource Returnsource; Try{Returnsource=Imaging.createbitmapsourcefromhbitmap (BMP. Gethbitmap (), IntPtr.Zero, Int32rect.empty, BITMAPSIZEOPTIONS.F            Romemptyoptions ()); }            Catch{Returnsource=NULL; }            returnReturnsource; }
      

3. Conversion from icon to ImageSource

        /// <summary>        /// convert        from icon to ImageSource /// </summary>          Public imagesource changeicontoimagesource (icon icon)        {            = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon (                icon). Handle,                int32rect.empty,                bitmapsizeoptions.fromemptyoptions ());            return imagesource;        }

4. Conversion from icon to ImageSource

 Internal Static classiconutilities {[DllImport ("Gdi32.dll", SetLastError =true)]            Private Static extern BOOLDeleteObject (IntPtr hobject);  Public StaticImageSource Toimagesource ( Thisicon icon) {Bitmap Bitmap=icon.                Tobitmap (); INTPTR Hbitmap=bitmap.                Gethbitmap (); ImageSource Wpfbitmap=Imaging.createbitmapsourcefromhbitmap (Hbitmap, IntPtr.Zero,                Int32rect.empty, Bitmapsizeoptions.fromemptyoptions ()); if(!DeleteObject (HBITMAP)) {                    Throw Newwin32exception (); }                returnWpfbitmap; }            //This is no additional conversion,:)             Public StaticImageSource Toimagesource ( Thisicon icon) {ImageSource ImageSource=Imaging.createbitmapsourcefromhicon (icon.                Handle, Int32rect.empty, Bitmapsizeoptions.fromemptyoptions ()); returnImageSource; }        }

Call: ImageSource Wpfbitmap = img. Toimagesource ();

Reference: Http://stackoverflow.com/questions/1127647/convert-system-drawing-icon-to-system-media-imagesource

5. Converting from ImageSource to Bitmap is to remove the Urisource.localpath from the ImageSource and then use the new Bitmap (FileName) method to get it. The other way I haven't found it yet

//System.Windows.Controls.Image Imguserheadfacecutedit;stringSTR1 = ((BitmapImage) (Imguserheadfacecutedit.source)). Urisource.absolutepath;//This path is not recognized by new Bitmap (STR1)stringSTR2 =( (BitmapImage) (Imguserheadfacecutedit.source)). Urisource.localpath; //Bitmap sourceimage = new Bitmap (sourceimageuri.tostring ());stringSTR3 =Strimgsourcefilename; Console.WriteLine ("Absolutepath ="+str1); Console.WriteLine ("LocalPath ="+str2); Console.WriteLine ("Srcefilename ="+ STR3);

This is the result of the operation:

Absolutepath =c:/documents%20and%20settings/zp/%e6%a1%8c%e9%9d%a2/%e6%a1%8c%e9%9d%a2%e7%a7%80/10111411409225. Jpg

LocalPath =c:\documents and settings\zp\ desktop \ Desktop Show \10111411409225.jpg

Srcefilename =c:\documents and settings\zp\ desktop \ Desktop Show \10111411409225.jpg

Who found the implementation method, leave a message ah

This article from zoop89850 's blog, the original address: http://www.cnblogs.com/zp89850/archive/2011/10/27/2226039.html

Conversion of ImageSource and bitmap in WPF image control

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.