Silverlight 4: How to Capture Video from Default Webcam?


In this post I will demonstrate one of the new feature of Silver-light 4: “Accessing Default Webcam using Silver-light 4”.

Silverlight 4 Beta 1 has been released by Microsoft on 18th November 2009. There are lots of goodies 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 feature “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 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) & create an instance of CaptureSource. Then call TryCaptureDevice() to initiate the Video Capture. This first get the default Video Capture device & assign it to the VideoBrush instance of the rectangle. Remember that, this will ask the user to grant permission to the user device & upon successful 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 has already access to the device or grant access from the user
if (CaptureDeviceConfiguration.AllowedDeviceAccess || CaptureDeviceConfiguration.RequestDeviceAccess())
{
btnPlayCapture.IsEnabled = true;
btnStopCapture.IsEnabled = false;
btnCaptureDevice.IsEnabled = false;
}
}
}


Attachments

  • Download Sample Source Code (36332-7944-SL420Webcam20Demo20Solution1.zip)
  • Comments

    Author: PHANI HARSHITHA MADALA09 Feb 2010 Member Level: Gold   Points : 1

    Hi Mr.kunal,

    Very usefull info..

    Thanks for sharing with all of us.


    regards,

    harshitha

    Author: Kunal Chowdhury09 Feb 2010 Member Level: Gold   Points : 1

    Hi Harshita,

    Thanks for wishing me. Nice to hear that it helped you.

    Thanks & Regards,
    Kunal Chowdhury | Software Developer | Chennai | India
    Follow My Blog Post: http://kunal2383.blogspot.com
    Follow My Tweets : http://twitter.com/kunal2383

    Author: BangaruBabu22 Feb 2010 Member Level: Gold   Points : 0

    HI ThanQ For Sharing

    Author: Kiran22 Feb 2010 Member Level: Gold   Points : 0

    Kunal,

    Nice sample,, thanks for sharing..


    Kiran

    Author: Kunal Chowdhury22 Feb 2010 Member Level: Gold   Points : 1

    Thank you guys for your feedbacks. I will try to post more articles in this site.

    Thanks & Regards,
    Kunal Chowdhury | Software Developer | Chennai | India
    Follow My Blog - http://kunal2383.blogspot.com
    Follow My Tweets - http://twitter.com/kunal2383



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: