Webcam Support in Silverlight 4 to capture Video

Posted by: Suprotim Agarwal , on 4/10/2011, in Category Silverlight 2, 3, 4 and 5
Views: 80010
Abstract: Silverlight 4 support for audio capture devices and video capture devices (Webcam), has opened up a new range of possibilities like recording, snapshot etc. In this short article, we will see how simple it is to use a Webcam in Silverlight 4.

Silverlight 4 support for audio capture devices and video capture devices (Webcam), has opened up a new range of possibilities like audio/video recording, snapshots etc. You can also use the Webcam feature for Video conferencing but since there is limited support for compression and encoding available out-of-the-box, it is not yet a very viable solution for conferencing. In this short article, we will see how simple it is to use a Webcam in Silverlight 4.

Silverlight automatically recognizes the video and audio capture devices on your machine. If you want to explicitly list the audio and video devices for the end user to choose from, read my blogpost Silverlight 4: List Audio and Video Devices

Follow these steps:

Step 1: Create a new Silverlight 4 application and call it ‘SilverlightWebCam’.

Step 2: In the MainPage.xaml, add the following controls:


Step 3: In the code behind, create a CaptureSource object as a member field, so that it is available for all events. The CaptureSource class is an important class that provides methods that work with specific audio or video captures from the associated capture device.

private CaptureSource _cs = null;

Now create the event handlers for the Start and Stop button


Step 4: In the btnStartWebcam event handler, we are making a call to the StartWebCam() method. Let us code this method, as shown below:


In a sandboxed Silverlight application, the application must request access to the webcam, before it can be used. So we use the boolean AllowedDeviceAccess to check if the user has given your application the permission to use the audio and video capture features. If not, the application requests permission (as shown below) by calling the CaptureDeviceConfiguration.RequestDeviceAccess() function.


Step 5: The next step is to get the default webcam, by making a call to the GetDefaultVideoCaptureDevice method of the CaptureDeviceConfiguration class.

Note: Read my post Silverlight 4: List Audio and Video Devices to explicitly list the audio and video devices on your machine

Step 6: Once you have a capture device, display the captured video by creating a VideoBrush, and set its source to the CaptureSource. The VideoBrush can then be used as the Fill brush for a destination control (a Rectangle in our case) to display the video to the user.


You can also stop the Webcam by calling the Stop() method on the CaptureSource


Note: We are using the default resolution and video format. If you want more control over these parameters, check the VideoFormat class.

I hope you liked this article and I thank you for viewing it. The entire source code of this article can be viewed over here

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for Fifteen consecutive years. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.

Get in touch with him on Twitter @suprotimagarwal or at LinkedIn

Page copy protected against web site content infringement 	by Copyscape

Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by rams on Tuesday, April 12, 2011 4:03 AM
hi! how can i stop webcam or release all multimedia resources from a different silverlight window or usercontrol? starting and stopping webcam within the same window works fine. but after starting the webcam and navigating to a different usercontrol window, am not able to stop webcam from there. working around with a static variable of type capturesource, i was able to stop it. but i would prefer doing in without using "static". any help please???
Comment posted by Petteri Lehtonen on Tuesday, April 12, 2011 11:03 AM
How to capture video and save it on a server? This is kinda useless without the possibility to record the video?
Comment posted by Suprotim Agarwal on Thursday, April 14, 2011 5:04 AM
rams: Currently I cannot think of a neat way. Let me try this over the weekend and get back to you.

Petteri: Very true! I will write an article soon on the same, as soon as I am up and running (currently down with viral)
Comment posted by Neil on Tuesday, June 14, 2011 3:46 PM
Is there a WPF version of this class?  Seems like this is Silverlight only.
Comment posted by prasad on Tuesday, January 31, 2012 3:08 AM
it s some much useful for me
Comment posted by mightyj on Sunday, February 10, 2013 9:15 AM
this tutorial was great but there are still something missing. Recording of the video to make it perfect
Comment posted by gatesnext on Thursday, August 8, 2013 1:03 AM
Nice article
Comment posted by sanjeev agarwal on Monday, August 26, 2013 11:54 PM
really wonderful and nice explanation
Comment posted by Görkem Bendin on Friday, November 29, 2013 7:54 AM
Thank you very much :)
Comment posted by Chirag on Friday, December 19, 2014 3:51 AM
how to save video that has been recorded through webcam using silverlight
in client machine