title: Video preview in C#
body: <p>I have a webcam capture app and I'm trying to implement a video preview in my WPF UI.  Capturing happens in a C++ DLL.  I have a current solution but I'm not too happy with it.  The main goal is that the video preview in the UI doesn't interfere with the C++ DLL much, as it has to compress the video frames and write them to disk.  I won't be able to display every frame, because the compression is fairly CPU-intensive.</p>
  
  <p>My current video preview solution is:</p>
  
  <ul>
  <li><p>An Image control is used in my window, and its Source attribute is data-binded to a BitmapSource called VideoPreviewSource</p></li>
  <li><p>I set up a System.Threading.TimerCallback to update my preview.  The timer callback function dispatches to a thread which can update the UI (using this.Dispatcher.BeginInvoke) which calls UpdatePreview()</p></li>
  <li><p>UpdatePreview() asks the C++ DLL for a video frame.  The call to the DLL puts the image's raw data in byte[] _rawImageData, which I allocate once in my window's constructor.  </p></li>
  <li><p>UpdatePreview() then sets VideoPreviewSource to: BitmapSource.Create(width, height, 96, 96, pf, null, _rawImageData, width * 4);  Finally OnPropertyChanged("VideoPreviewSource") is called.  </p></li>
  </ul>
  
  <p>It seems like if I make the timer more frequent, garbage collection is called less often and it basically looks like a memory leak.  </p>
  
  <p>Any better approach to a video preview?</p>