Click here to Skip to main content
13,451,773 members (45,854 online)
Click here to Skip to main content
Add your own
alternative version


8 bookmarked
Posted 19 Nov 2009

Silverlight 4: How to Capture Video from Default Webcam?

, 21 Nov 2009
Rate this:
Please Sign up or sign in to vote.
In this post, I will demonstrate one of the new features: “Accessing Default Webcam using Silverlight 4”.

Silverlight 4 Beta 1 has been released by Microsoft on 18th November 2009. There are lots of goodies that came up with the release of the new version. Among them, most of all are requested by the developers & users of Silverlight. In this post, I will demonstrate one of the new features “Accessing Default Webcam using Silverlight 4”.

To create a Silverlight 4 application, you need “Visual Studio 2010 Beta 2”. Download it from the Microsoft site. Then install the “Silverlight Tools 4 for Visual Studio 2010 Beta 2”. After successful installation, create a Silverlight 4 Application project.

Once you done with the project creation, Visual Studio will open the MainPage.xaml for you. Add a Rectangle & three Buttons inside the Grid. The Rectangle will be responsible for the Video output from your VideoCaptureDevice & buttons will be responsible for the interaction with the device. After adding the same, your XAML will look like this:

<Grid x:Name="LayoutRoot" Background="White">
     <StackPanel HorizontalAlignment="Center">
         <Rectangle x:Name="rectWebCamView" Width="500" Height="400"/>
         <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
             <Button x:Name="btnCaptureDevice" Content="Capture Device" Margin="5"/>
             <Button x:Name="btnPlayCapture" Content="Start Capture" Margin="5"/>
             <Button x:Name="btnStopCapture" Content="Stop Capture" Margin="5"/>

Now, go to the code behind file (MainPage.xaml.cs) & create an instance of CaptureSource. Then call TryCaptureDevice() to initiate the Video Capture. This first gets the default Video Capture device & assigns it to the VideoBrush instance of the rectangle. Remember that this will ask the user to grant permission to the user device & upon success only, it will start the device.

private void TryCaptureDevice()
    // Get the default video capture device
    VideoCaptureDevice videoCaptureDevice = 

    if (videoCaptureDevice == null)
        // Default video capture device is not setup
        btnPlayCapture.IsEnabled = false;
        btnStopCapture.IsEnabled = false;
        btnCaptureDevice.IsEnabled = true;

        MessageBox.Show("You don't have any default capture device");
        btnPlayCapture.IsEnabled = false;
        btnStopCapture.IsEnabled = false;

        // Set the Capture Source to the VideoBrush of the rectangle
        VideoBrush videoBrush = new VideoBrush();
        rectWebCamView.Fill = videoBrush;

        // Check if the Silverlight has already access
        // to the device or grant access from the user
        if (CaptureDeviceConfiguration.AllowedDeviceAccess || 
            btnPlayCapture.IsEnabled = true;
            btnStopCapture.IsEnabled = false;
            btnCaptureDevice.IsEnabled = false;


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Kunal Chowdhury «IN»
Software Developer (Senior)
India India
Kunal Chowdhury is a Microsoft "Windows Platform Development" MVP (Most Valuable Professional), a Codeproject Mentor, Telerik Developer Expert, Nokia Developer Champion, Windows 10 Champion, Microsoft Rockstar, Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows Store (WinRT) app development. He posts his findings, articles, tutorials in his technical blog and CodeProject.

Book authored:

Connect with me on:

My CodeProject Awards/Prizes:

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01-2016 | 2.8.180318.3 | Last Updated 21 Nov 2009
Article Copyright 2009 by Kunal Chowdhury «IN»
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid