Saturday, January 08, 2011

Webcam or other video device access using Silverlight 4.0

Hello friends,

Microsoft Silverlight 4.0 provided another great feature of Audio & Video capturing. Today I am interested in using one of these cool features which is video capturing.
With the help of the video & audio capturing feature we can get all the capturing devices and use them. In current example there is only one source which is my laptop webcam.



So we are using 8 simple steps. With the help this simple we are capable of capturing webcam and stop capturing.

Step 1:- Create a new project of Silverlight with version Silverlight 4 version.
Step 2:- Draw a shape either rectangle or eclipse. The shape will hold the video.
Step 3:- CaptureDeviceConfiguration provides GetAvailableVideoCaptureDevice & GetDefaultvideoCaptureDevice methods.
The GetAvailableVideoCaptureDevice list out the entire available video capture device attached with your machine and GetDefaultVideoCaptureDevice provides default video capture device.

But before accessing any device from any machine we need to request device access and need to determine whether the device access is allowed or not. For determining the permission or request a device access we have following 2 methods
CaptureDeviceConfiguration.AllowedDeviceAccess CaptureDeviceConfiguration.RequestDeviceAccess

It will prompt message for accessing the capturing device when you run the program.

var webCam = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();



Step 4:-
Once you determine the capture device configuration then you need to assign it to assign as a video capture source device of capturesource.
captureSource = new CaptureSource();

Step 5:- Once you assign the capture source’s video capture device then use a new brush utility provided by Silverlight which is Video Brush. You just need to set source of video Brush which will be your capture source.
var videoBrush = new VideoBrush();
videoBrush.SetSource(captureSource);

Step 6:- Once the video Brush’s source is set then you can fill any shape with video brush.
rectangle1.Fill = videoBrush;
ellipse1.Fill = videoBrush;

Step 7:- now to start capturing from video source you need to call start method of capture source.
captureSource.Start();
Step 8:- you can stop capturing video by stop method.

captureSource.Stop();
From the above description you can capture video from Webcam or any other video device which attached with your machine using Silverlight video capture facility. One more advantage is that you can take image also means image capturing is also possible. For this you have to define asynchronous image capture event.


In this way you can access video capture device. More than this you can download code from
download source code

Enjoy Silverlight.


Thanks & Regards
Rajat Jaiswal