Silverlight 4 is now not only more mature but also comes with rich features. Most of these features were recommendations from Silverlight community.
Webcam support is one of them. This was a very highly requested feature and has now been included in this version.
The following steps will get you up and running for the initial adventure:
The first step is to get all the available capture devices (webcams, microphones, etc.) or default capture device on the system. So
CaptureDeviceConfiguration class helps us to obtain information about all available devices. This helper class exposes a number of
static members for this purpose.
It returns a collection of video devices on the system.
All the audio devices.
Default audio device.
Default video device.
So after the first step, we got the device
Before any interaction with the device, Silverlight application needs permission from the user (the user must grant permission in the security prompt and that way, it ensures the call is safe).
CaptureDeviceConfiguration.RequestDeviceAccess(): To request access, call this
static method in response to user initiated event like
listbox item selection, button click, etc. If your application automates this call without user interaction (i.e.,
Load event), the method will return
false and further action by application will throw an
Okay, the application got the permission. Let's proceed with the next step.
Now we have permission and the device handy. It's time for the application to interact with the device using
CaptureSource class. This class allows you to collect the video feed from the camera. To make this happen, create a
CaptureSource object and set the
VideoCaptureDevice to the selected device. With the help of
CaptureSource, the application is now capable of performing the following task on the devices.
- State of the device
- Capturing single video frame
Now create a video brush. A
VideoBrush gives us the ability to paint any area or controls with video content. At this stage, we use
CaptureSource as a source of our brush.
The next thing we need to do is to call
CaptureSource.Start() method to begin capturing our live video.
Finally, we'll set the
Fill property of a
Rectangle object to this brush.
Now we will code a very simple application:
- Create a Silverlight application and open the MainPage.xaml.
- Add one rectangle (for display purpose) and two buttons (start and stop):
<Grid x:Name="LayoutRoot" Background="White">
<Rectangle Height="256" Name="myDisplay" Stroke="Black"
StrokeThickness="1" Width="584" />
<StackPanel Name="subStackpanel" Orientation="Horizontal"
<Button x:Name="butStart" Content="Start" Click="butStart_Click"/>
<Button x:Name="butStop" Content="Stop" Click="butStop_Click" />
- Code behind as follows:
public partial class MainPage : UserControl
private void butStart_Click(object sender, RoutedEventArgs e)
VideoCaptureDevice myWebcam =
if (CaptureDeviceConfiguration.AllowedDeviceAccess ||
captureSourec = new CaptureSource();
captureSourec.VideoCaptureDevice = myWebcam;
VideoBrush myBrush = new VideoBrush();
myBrush.Stretch = Stretch.UniformToFill;
myDisplay.Fill = myBrush;
private void butStop_Click(object sender, RoutedEventArgs e)
if (captureSourec != null)