MediaKit and MVVM

Aug 12, 2010 at 1:15 PM

How can I bind VideoCaptureElement to a ViewModel?

I need to be able to capture an image from the web camera and store this in a database.

Aug 12, 2010 at 8:34 PM
Edited Aug 12, 2010 at 8:39 PM


Hi, I've done this by putting a property in the ViewModel to store the VideoCaptureElement and then bind a WPF ContentControl to that property. Something like this:

In the ViewModel:

This is a property to store the VideoCaptureElement.

VideoCaptureElement _videoElement;
public VideoCaptureElement VideoElement
                return _videoElement;

Then you need to initialize the VideoCaptureElement (you can do it in the ctor of the ViewModel) like this:

_videoElement = new VideoCaptureElement();
_videoElement.FPS = 25;
_videoElement.EnableSampleGrabbing = true;
_videoElement.VideoCaptureDevice = _device;
_videoElement.Width = 300;
_videoElement.Height = 240;
_videoElement.Stretch = System.Windows.Media.Stretch.Fill;
_videoElement.NewVideoSample += new EventHandler<WPFMediaKit.DirectShow.MediaPlayers.VideoSampleArgs>(_videoElement_NewVideoSample);


In the View:

<ContentControl Background="White" HorizontalAlignment="Left" VerticalAlignment="Top" Content="{Binding VideoElement}" Height="240" Width="300" />


Hope that helps.

- Gonzalo

Aug 13, 2010 at 9:29 AM

Of course!!! Brain was starting to hurt last night trying to get this sorted... Many thanks

Aug 13, 2010 at 10:48 AM

Another quick question hopefully... What is the easiest way to capture an image from the webcam? I've seen the previous post about this but was wondering if there was an updated way??

Aug 13, 2010 at 2:11 PM

Glad I could help!

Regarding the image capture, I do it by hooking in the NewVideoSample event and getting each video frame using VideoSampleArgs.VideoFrame. Whenever the user captures the image (stops the VideoCaptureElement), I have the latest video frame stored in memory. I saw there's another way but I can't tell which one is better.