Click here to Skip to main content
Click here to Skip to main content

Silverlight 4: Capturing Video from Default Webcam

, 20 Nov 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
Silverlight 4 Beta 1 has been released by Microsoft on 18th November 2009. Among the new features, I will demonstrate one of them “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 are requested by the developers and 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.

Code of Interest

Once you are 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) and 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 and 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 already has access 
        // to the device or grant access from the user
        if (CaptureDeviceConfiguration.AllowedDeviceAccess || 
            btnPlayCapture.IsEnabled = true;
            btnStopCapture.IsEnabled = false;
            btnCaptureDevice.IsEnabled = false;


  • 20th November, 2009: Initial post


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


About the Author

Kunal Chowdhury (@kunal2383)
Team Leader
India India
Kunal Chowdhury is a Microsoft "Windows Platform Development" MVP (Most Valuable Professional), a Codeproject Mentor, Telerik MVP, Nokia Developer Champion, 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.
Technical Blog:
Twitter :
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

Questionplease update code according to the latest version of silverlite . Pinmembermahaveer singh ratnoo16-Mar-13 11:00 
QuestionError: PinmemberMrNilesh2-Dec-12 23:26 
AnswerRe: Error: Pinmvp_ Kunal Chowdhury _3-Dec-12 3:20 
GeneralSave the webcam video and audio Pinmembercool2b9-Aug-12 4:00 
GeneralRe: Save the webcam video and audio PinmemberRahul.PP7-Jan-13 2:56 
Questionrecording video from webcam in without Silver light Pinmembermiss_sumaira26-Mar-12 23:43 
GeneralMy vote of 4 Pinmemberseanxywu@gmail.com31-Jan-12 9:42 
GeneralMy vote of 4 PinmemberAbhinav S29-Sep-10 5:59 
GeneralMy vote of 2 Pinmemberdisore24-Nov-09 21:49 
GeneralRe: My vote of 2 PinmemberKunalChowdhury15-Feb-10 20:10 
General5 From Me Pinmembermheidari24-Nov-09 2:37 
GeneralRe: 5 From Me PinmemberKunalChowdhury15-Feb-10 20:11 
Generalsilverlight video PinmemberMember 975084115-May-13 20:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150123.1 | Last Updated 20 Nov 2009
Article Copyright 2009 by Kunal Chowdhury (@kunal2383)
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid