title: WPF, Displaying Emgu Image, using Binding
body: <p>I am familiar with basic code to display an Emgu image in a WPF image box, when all the source code is in the MainWindow.xaml.cs code-behind.</p>
  
  <p>However I am trying to place my Emgu-related code, including the "ProcessFrame" event / Queryframe snippet, into a separate class of static methods so that I can reuse them later. I am doing this because while I will want to be able to grab images from the same camera at a later stage, I also want the flexibility to display those images in a different image box. I am having trouble with this step. </p>
  
  <p>If I could bind the Image box dynamically to a property in the static method (and also enable / disable that binding programmatically), I think that would solve my problem. However, there may be some other problem with the approach I am trying to take. Any code / xaml modifications greatly appreciated.</p>
  
  <p>The following code works, but is unsatisfactory because it forces me to bundle ProcessFrame method into the MainWindow code:</p>
  
  <p>XAML (working):</p>
  
  <pre><code>&lt;Window x:Class="EmguWPF_Test.MainWindow"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       Title="MainWindow" Height="350" Width="525"&gt;
       &lt;Grid&gt;
         &lt;Image Height="215" HorizontalAlignment="Left" Margin="62,66,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="224"  /&gt;
       &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>MainWindow Code Snippet (working):</p>
  
  <pre><code>//using statements etc
  public partial class MainWindow : Window
  {
  private Image&lt;Bgr, Byte&gt; image; 
  private Capture capture = null;
  
  private void button1_Click(object sender, RoutedEventArgs e)
  {
      InitializeCameras();
      timer = new DispatcherTimer();
      timer.Tick+=new EventHandler(ProcessFrame);
      timer.Interval = new TimeSpan(0, 0, 0, 0, 10);
      timer.Start();            
  }
  
  private void InitializeCameras()
      {
          if (capture == null)
          {
              try
              {
                  capture = new Capture(0);
              }
              catch // etc 
          }
      }
  
  private void ProcessFrame(object sender, EventArgs arg)
      {
          image = capture.QueryFrame();
          image1.Source = BitmapSourceConvert.ToBitmapSource(image);
      }
  }
  
  public static class BitmapSourceConvert
  {
     [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>The following code is where I am up to but need help:</p>
  
  <p>XAML (same as before)</p>
  
  <pre><code>&lt;Window x:Class="EmguWPF_Test.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" Height="350" Width="525"&gt;
  
  &lt;Grid&gt;
      &lt;Image Height="215" HorizontalAlignment="Left" Margin="62,66,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="224"  /&gt;
  &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>ViewModel Snippet (yes - perhaps too ambitious to be experimenting with design patterns):</p>
  
  <pre><code>public ViewModel()
      {    
          CaptureMethods.InitializeCameras();
          timer = new DispatcherTimer();
          timer.Tick += new EventHandler(CaptureMethods.ProcessFrame);
          timer.Interval = new TimeSpan(0, 0, 0, 0, 10);
          timer.Start(); 
      }
  </code></pre>
  
  <p>CaptureMethods class, not working as a separate class in the way I want it to. You will notice I am now defining the capture field in this class, not in the ViewModel class:</p>
  
  <pre><code>class CaptureMethods
  {
      private static Capture capture = null;
  
      public static void InitializeCameras()
      {
          if (capture == null)
          {
              try
              {
                  capture = new Capture(0);
              }
              catch // etc 
          }
      }
  
  public static void ProcessFrame(object sender, EventArgs arg)
      {
          image = capture.QueryFrame();
          image1.Source = BitmapSourceConvert.ToBitmapSource(image); // this is my problem line
      }
  }
  
  // BitmapSourceConvert class not repeated here to avoid duplication.
  </code></pre>
  
  <p>Thanks!</p>