Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Silverlight 4: Capturing Video from Default Webcam

0.00/5 (No votes)
20 Nov 2009 1  
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:

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

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here