Click here to Skip to main content
15,400,390 members
Articles / Desktop Programming / XAML
Article
Posted 20 Nov 2009

Stats

67.7K views
5.2K downloads
23 bookmarked

Silverlight 4: Capturing Video from Default Webcam

Rate me:
Please Sign up or sign in to vote.
4.57/5 (11 votes)
20 Nov 2009CPOL1 min read
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”.

Introduction

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”.

Background

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:

XML
<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"/>
        </StackPanel>
    </StackPanel>
</Grid>

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.

C#
private void TryCaptureDevice()
{
    // Get the default video capture device
    VideoCaptureDevice videoCaptureDevice = 
    	CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();
    
    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");
    }
    else
    {
        btnPlayCapture.IsEnabled = false;
        btnStopCapture.IsEnabled = false;
        
        // Set the Capture Source to the VideoBrush of the rectangle
        VideoBrush videoBrush = new VideoBrush();
        videoBrush.SetSource(captureSource);
        rectWebCamView.Fill = videoBrush;
        
        // Check if the Silverlight already has access 
        // to the device or grant access from the user
        if (CaptureDeviceConfiguration.AllowedDeviceAccess || 
        	CaptureDeviceConfiguration.RequestDeviceAccess())
        {
            btnPlayCapture.IsEnabled = true;
            btnStopCapture.IsEnabled = false;
            btnCaptureDevice.IsEnabled = false;
        }
    }
}

History

  • 20th November, 2009: Initial post

License

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

Share

About the Author

Kunal Chowdhury «IN»
Technical Lead
India India

Kunal Chowdhury is a former Microsoft "Windows Platform Development" MVP (Most Valuable Professional, 2010 - 2018), a Codeproject Mentor, Speaker in various Microsoft events, Author, passionate Blogger and a Senior Technical Lead 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 app development. He posts his findings, articles, tutorials in his technical blog (www.kunal-chowdhury.com) and CodeProject.


Books authored:


Connect with Kunal on:





Comments and Discussions

 
Questionplease update code according to the latest version of silverlite . Pin
mahaveer singh ratnoo16-Mar-13 10:00
Membermahaveer singh ratnoo16-Mar-13 10:00 
QuestionError: Pin
MrNilesh2-Dec-12 22:26
MemberMrNilesh2-Dec-12 22:26 
AnswerRe: Error: Pin
Kunal Chowdhury «IN»3-Dec-12 2:20
professionalKunal Chowdhury «IN»3-Dec-12 2:20 
GeneralSave the webcam video and audio Pin
cool2b9-Aug-12 3:00
Membercool2b9-Aug-12 3:00 
GeneralRe: Save the webcam video and audio Pin
Rahul.PP7-Jan-13 1:56
MemberRahul.PP7-Jan-13 1:56 
Questionrecording video from webcam in asp.net without Silver light Pin
miss_sumaira26-Mar-12 22:43
Membermiss_sumaira26-Mar-12 22:43 
GeneralMy vote of 4 Pin
seanxywu@gmail.com31-Jan-12 8:42
Memberseanxywu@gmail.com31-Jan-12 8:42 
GeneralMy vote of 4 Pin
Abhinav S29-Sep-10 4:59
MemberAbhinav S29-Sep-10 4:59 
GeneralMy vote of 2 Pin
FantasticFiasco24-Nov-09 20:49
MemberFantasticFiasco24-Nov-09 20:49 
GeneralRe: My vote of 2 Pin
Kunal Chowdhury «IN»15-Feb-10 19:10
professionalKunal Chowdhury «IN»15-Feb-10 19:10 
General5 From Me Pin
mheidari24-Nov-09 1:37
Membermheidari24-Nov-09 1:37 
GeneralRe: 5 From Me Pin
Kunal Chowdhury «IN»15-Feb-10 19:11
professionalKunal Chowdhury «IN»15-Feb-10 19:11 
Generalsilverlight video Pin
Member 975084115-May-13 19:34
MemberMember 975084115-May-13 19:34 

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

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