Incorporating Video into a Silverlight 4 Application

Over the past week, I spent some time with Silverlight 4. I’m really impressed by how easy it is to incorporate video into an application. This post shows how to capture video from the camera and display the video on the screen. The post also handles grabbing single frames of video. You might use this type of arrangement to allow users to upload images, hold impromptu web casts, and to do video conferencing.

I’ve uploaded a barebones application that turns on video capture and displays the video (viewable here)

To understand how this works, you need to be familiar with the following objects:




The CaptureSource type encapsulates the methods needed to interact with audio and video devices.

CaptureDeviceConfiguration acts as gatekeeper to the webcam and microphone. CaptureDeviceConfiguration knows how to ask the user for permission to use the webcam or microphone. It also remembers what the user said about using the webcam or microphone during the current session.

VideoBrush knows how to paint using a video as the source.

In order to interact with the camera, you need to know whether the user gave you permission and if not, you have to ask for permission. Once you have permission, you just need to capture video from the default camera, create the brush, and start capturing the video.

private CaptureSource _captureSource = null;
private void btnStartCapture_Click(object sender, RoutedEventArgs e)
    if (_captureSource != null && 
        _captureSource.State == CaptureState.Started)

    _captureSource = new CaptureSource
         VideoCaptureDevice = 
    var brush = new VideoBrush();
    rectVideo.Fill = brush;

Later on, when the user wants to capture the current video as an image, you just do something like this to put the current frame into an image control:


private void btnGrabImage_Click(object sender, RoutedEventArgs e)
    if(_captureSource == null || _captureSource.State != CaptureState.Started)
        image => imgCapture.Source = image

As someone who spent way too much time in the C++/MFC era writing code to capture video from devices, I am incredibly impressed with the brevity of the code. I think the SL4 team did an awesome job here. Way to go!


You can grab the sample project here.

Posted 01-25-2010 6:00 AM by Scott Seely


About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Google Reader or Homepage Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of
Red-Gate Tools For SQL and .NET


SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
NHibernate Profiler
Balsamiq Mockups
JetBrains - ReSharper
Web Sequence Diagrams
Ducksboard<-- NEW Friend!


Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers


Community Server (Commercial Edition)