title: Face Detection with Emgu CV in C# and WPF
body: <p>I am doing Face Detection with <code>Emgu CV</code> in C# and <code>WPF</code>,the problem is my program is getting run,but nothing is displayed in the window-</p>
  
  <p>CODE IS-</p>
  
  <pre><code>MainWindow.xaml
  
  &lt;Window x:Class="FaceRecognition.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="MainWindow" Height="600" Width="800" Loaded="Window_Loaded"&gt;
      &lt;Grid&gt;
          &lt;Image Name="image1" Stretch="Fill"&gt;&lt;/Image&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  
  MainWindow.xaml.cs
  
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  using Emgu.CV.Structure;
  using Emgu.CV;
  using System.Runtime.InteropServices;
  using System.Windows.Threading;
  using System.Drawing;
  
  namespace FaceRecognition
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          private Capture capture;
          private HaarCascade haarCascade;
          DispatcherTimer timer;
          public MainWindow()
          {
              InitializeComponent();
          }
  
          private void Window_Loaded(object sender, RoutedEventArgs e)
          {
              capture = new Capture();
              haarCascade = new HaarCascade(@"haarcascade_frontalface_default.xml");
              timer = new DispatcherTimer();
              timer.Tick += new EventHandler(timer_Tick);
              timer.Interval = new TimeSpan(0, 0, 0, 0, 1);
              timer.Start();
          }
  
  
          void timer_Tick(object sender, EventArgs e)
          {
              Image&lt;Bgr, Byte&gt; currentFrame = capture.QueryFrame();
  
              if (currentFrame != null)
              {
                  Image&lt;Gray, Byte&gt; grayFrame = currentFrame.Convert&lt;Gray, Byte&gt;();
  
                  var detectedFaces = grayFrame.DetectHaarCascade(haarCascade)[0];
  
                  foreach (var face in detectedFaces)
                      currentFrame.Draw(face.rect, new Bgr(0, double.MaxValue, 0), 3);
  
                  image1.Source = ToBitmapSource(currentFrame);
              }
  
          }
  
  
          [DllImport("gdi32")]
          private static extern int DeleteObject(IntPtr o);
  
          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;
              }
          }
      }
  }
  </code></pre>
  
  <p>I have also added all the references required of <code>Emgu dll</code> files,and also added all the <code>openCV</code> dll files in the following folder-</p>
  
  <pre><code>D:\dot net\handouts\handoutsprogram\FaceRecognition\FaceRecognition\bin\Debug
  </code></pre>