Webcam Support in Silverlight 4 to capture Video
Posted by: Suprotim Agarwal
in Category Silverlight 2, 3, 4 and 5
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.
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!