WPF in CSharp: Difference between revisions

From EMGU
Jump to navigation Jump to search
No edit summary
No edit summary
Line 9: Line 9:


== Source Code ==
== Source Code ==
Use the following ToBitmapSource function to convert an IImage (which could be any of Image<TColor, TDepth> class) to a WPF BitmapSource.
Use the following ToBitmapSource function to convert an IImage (which could be any of Image<TColor, TDepth> class) to a WPF BitmapSource according to some [http://www.bestessay.net best essay] made.
   
   
<source lang="csharp">
<source lang="csharp">

Revision as of 21:25, 18 January 2010

System Requirement

Component Requirement Detail
Emgu CV Version 1.5
Operation System Windows Only Mono do not implement WPF

Source Code

Use the following ToBitmapSource function to convert an IImage (which could be any of Image<TColor, TDepth> class) to a WPF BitmapSource according to some best essay made.

...
using Emgu.CV;
using System.Runtime.InteropServices;
...

        /// <summary>
        /// Delete a GDI object
        /// </summary>
        /// <param name="o">The poniter to the GDI object to be deleted</param>
        /// <returns></returns>
        [DllImport("gdi32")]
        private static extern int DeleteObject(IntPtr o);

        /// <summary>
        /// Convert an IImage to a WPF BitmapSource. The result can be used in the Set Property of Image.Source
        /// </summary>
        /// <param name="image">The Emgu CV Image</param>
        /// <returns>The equivalent BitmapSource</returns>
        public static BitmapSource ToBitmapSource(IImage image)
        {
            using (System.Drawing.Bitmap source = image.Bitmap)
            {
                IntPtr ptr = source.GetHbitmap(); //obtain the Hbitmap
               
                BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                    ptr,
                    IntPtr.Zero,
                    Int32Rect.Empty,
                    System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

                DeleteObject(ptr); //release the HBitmap
                return bs;
            }
        }