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

Silverlight 4: Capturing Video from Default Webcam

By , 20 Nov 2009
 

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

_ Kunal Chowdhury _
Software Developer
India India
Member
Kunal Chowdhury is a Microsoft MVP (Most Valuable Professional) in Silverlight Technology, a Codeproject MVP & Mentor, DZone MVB (Most Valuable Blogger), Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer by profession.
 
He is currently working as a Software Engineer II in an MNC located at Pune, India. He has a very good skill over XAML, C#, Silverlight and WPF. He has a good working experience in Windows 7 application (including Multi-touch) development too.
 
He posts his findings in his technical blog. He also writes for SilverlightShow and Codeproject portal. Many of his articles were highlighted as "Article of the Day" in Microsoft sites.
 
He also has another website called Silverlight-Zone.com where he posts article links on Silverlight, Windows Phone 7 and XNA accumulated from various web sites to help the community grow on specified technologies.
 
You can reach him in his Blog : http://www.kunal-chowdhury.com
He is also available in Twitter : http://twitter.com/kunal2383

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionplease update code according to the latest version of silverlite .membermahaveer singh ratnoo16 Mar '13 - 10:00 
QuestionError:memberMrNilesh2 Dec '12 - 22:26 
AnswerRe: Error:mvp_ Kunal Chowdhury _3 Dec '12 - 2:20 
GeneralSave the webcam video and audiomembercool2b9 Aug '12 - 3:00 
GeneralRe: Save the webcam video and audiomemberRahul.PP7 Jan '13 - 1:56 
Questionrecording video from webcam in asp.net without Silver lightmembermiss_sumaira26 Mar '12 - 22:43 
GeneralMy vote of 4memberseanxywu@gmail.com31 Jan '12 - 8:42 
GeneralMy vote of 4memberAbhinav S29 Sep '10 - 4:59 
GeneralMy vote of 2memberdisore24 Nov '09 - 20:49 
GeneralRe: My vote of 2memberKunalChowdhury15 Feb '10 - 19:10 
Hi disore,
Thank you for your feedback. I wrote this article for the community who wants to learn the video capturing functionality in Silverlight 4. Can you tell me how I can improve it? This will be a great help.
Thank you once again.
Regards - Kunal Chowdhury | Software Developer | Chennai | India | My Blog | My Tweets

General5 From Memembermheidari24 Nov '09 - 1:37 
GeneralRe: 5 From MememberKunalChowdhury15 Feb '10 - 19:11 
Generalsilverlight videomemberMember 975084115 May '13 - 19:34 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 20 Nov 2009
Article Copyright 2009 by _ Kunal Chowdhury _
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid