Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 4601 |  Welcome, Guest!   Register  Login
Home > Articles > Silverlight > Silverlight 4: How to Capture Video from Default Webcam?

Silverlight 4: How to Capture Video from Default Webcam?

Article posted by Kunal2383 on 3/1/2010 | Views: 9377 | Category: Silverlight | Level: Beginner red flag


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

Download


 Download source code for Silverlight 4: How to Capture Video from Default Webcam?


Introduction

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


Pre-Requisite

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.


XAML Steps

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>


Code Steps

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;
}
}
}
Conclusion
This is a sample application to showcase the new Webcam feature in Silverlight 4. This can be modified to save the image snapshot from the webcam.

Download Sample Solution:   Silverlight 4 Webcam Demo



If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:3 year(s)
Home page:http://www.kunal-chowdhury.com
Member since:Monday, March 01, 2010
Level:Starter
Status: [Member]
Biography:He is currently working as a Silverlight application developer. Has a very good skill over C#, XAML, Silverlight & WPF. He has a good working experience in Windows 7 application (including Multitouch) development.

During his professional career he worked in various technologies & delivered quality output. He never hesitates to take up challenges & work on the latest technologies in Microsoft platform.

He attended various software development competition & achieved different awards.

He is presently focusing on the RIA (Silverlight & WPF) & willing to become a Technology Specialist in Microsoft platform. Learning newer things, Blog posting & helping others in forums is one of his regular activity.

Specialties: Silverlight Application Development, WPF Application Development, Windows 7 Application Development
 Responses
Posted by: Prince.saurabh | Posted on: 14 Apr 2011 02:53:01 AM | Points: 25

Hi,

Its very nice article and i have done all this successfully
but it is not broadcasting to other system, if i publish the site and host at IIS and client access through browser then it is using client's web cam to show.

i want to broadcast this captured stream over web to all client.
Can u give some ideaz??
I have gone through Expression encoder but i dont want to buy. is there any free solution for this?????

Thanks

>> Write Response - Respond to this post and get points
Related Posts

In this article, we are going to learn how to list the data in Silverlight using ItemsControl and how to set element to element binding in Silverlight

In this article, I will demonstrate you guys how to animate the items in a specific direction in PathListBox control. This not only guide you for animation in PathListBox but also will help you understanding the StoryBoard creation (if you are very new to Silverlight). Read the whole article and at the end don’t forget to leave your comments.

This article describes how to consume ASP.NET web service in Silverlight in easy to follow steps.

Almost everyone doing development in Silverlight is talking about MVVM. This is the part 1 on the subject. I decided to split the articles into different parts as this tends to be a confusing subject for the beginners in the subject. What is good about this article is that I will cover the basics of MVVM and give you a full explanation on how you can retrieve, Edit, add, Update and delete data in MVVM and this is the only article I have seen so far that can do that. Another great thing about this article is that we are not going to use any Toolkit to enforce the rules of MVVM, So this is a Generic MVVM Article, you don’t need to install anything or download anything to follow this article.

In this article, we shall learn how to create custom splash (loading) screen in Silverlight.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2012 7:52:37 AM