Silverlight 4 – WebCam Support

Silverlight 4 introduces WebCam support which is probably the highlight of the Silverlight 4 release. The WebCam API is relatively simple to use, a webcam can be added, video captured and placed on the screen using the following procedure:

WebCam Video Capture Procedure

  1. Add a rectangle plus a button.
  2. Get a handle on the video capture device using CaptureDeviceConfiguration.
  3. Request access to the WebCam using CaptureDeviceConfiguration.RequestDeviceAccess().
  4. After the user clicks Yes at the prompt , create a CaptureSource object and set the VideoCaptureDevice to the selected webcam.
  5. Create a VideoBrush and set its source.
  6. Start the WebCam.
  7. Fill the Rectangle.

Code for WebCam Video Capture

//access the WebCam
VideoCaptureDevice webcamObj = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();

//user confirmation clips
if (CaptureDeviceConfiguration.RequestDeviceAccess())

CaptureSource captureSourceObj = new CaptureSource();
captureSourceObj.VideoCaptureDevice = webcamObj;

VideoBrush videoBrushObj = new VideoBrush();
videoBrushObj.SetSource(captureSourceObj);
videoBrushObj.Stretch = Stretch.UniformToFill;

//start WebCam
videoBrushObj.Start();

//fill the rectangle with the video
this.MyVideo.Fill = videoBrushObj;

This code only works for a single WebCam but CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices will return a list of all available webcams.

This entry was posted in FAQ and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>