First commit of the f# source code.
[master-thesis.git] / Parasitemia / WPF / BitmapSourceConverter.cs
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2004-2015 by EMGU Corporation. All rights reserved.
3 //----------------------------------------------------------------------------
4
5 using System;
6 using System.Runtime.InteropServices;
7 using System.Windows;
8 using System.Windows.Media;
9 using System.Windows.Media.Imaging;
10 using System.Windows.Shapes;
11 using Emgu.CV;
12
13 namespace Emgu.CV.WPF
14 {
15 public static class BitmapSourceConvert
16 {
17 /// <summary>
18 /// Delete a GDI object
19 /// </summary>
20 /// <param name="o">The poniter to the GDI object to be deleted</param>
21 /// <returns></returns>
22 [DllImport("gdi32")]
23 private static extern int DeleteObject(IntPtr o);
24
25 /// <summary>
26 /// Convert an IImage to a WPF BitmapSource. The result can be used in the Set Property of Image.Source
27 /// </summary>
28 /// <param name="image">The Emgu CV Image</param>
29 /// <returns>The equivalent BitmapSource</returns>
30 public static BitmapSource ToBitmapSource(IImage image)
31 {
32 using (System.Drawing.Bitmap source = image.Bitmap)
33 {
34 IntPtr ptr = source.GetHbitmap(); //obtain the Hbitmap
35
36 BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
37 ptr,
38 IntPtr.Zero,
39 Int32Rect.Empty,
40 System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
41
42 DeleteObject(ptr); //release the HBitmap
43 return bs;
44 }
45 }
46 }
47 }