Silverlight 4: New features overview (Part 1) – Webcam/Mic Support
This post starts a series of post about new features in Silverlight 4 Beta released at PDC 09.
Today I’ll show how to use Webcam/Mic support feature in the new version.
In order to use the Webcam/Mic the user should has valid drivers installed. To see which from installed drivers could be used open Silverlight Configuration and select 4th tab:
This tab also allows to select default webcam/mic to use with Silverlight applications.
Webcam/Mic controlled by CaptureSource class. This class allows to select which capture device to use (Video and Audio) and controls capturing.
In order to demonstrate the API I’ve created sample applciation. It will looks like follows:
Let’s see how to operate “Webcam On” button…
I’ve created class variable:
CaptureSource cs;
This variable will be used also for the second button handler, that’s why I’ve created it in class level.
In handler function, in order to use webcam/mic we have to get the end-user permission:
if (CaptureDeviceConfiguration.AllowedDeviceAccess ||
CaptureDeviceConfiguration.RequestDeviceAccess())
{
...
}
Those lines in runtime will render the standard Silverlight dialog box with a request to use webcam/mic. If user approves the request we will be able to use those devices:
In my sample I’ll use default video device, and will set it as a video capture source for my capture sources:
VideoCaptureDevice vcd = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();
//AudioCaptureDevice acd = CaptureDeviceConfiguration.GetDefaultAudioCaptureDevice(); //Could use it, but not needed heere
if (null != vcd /*&& null != acd*/)
{
cs = new CaptureSource();
cs.VideoCaptureDevice = vcd;
//cs.AudioCaptureDevice = acd;
...
When everything is set I’ll start capturing.
cs.Start();
I’ll use the capture source as a source for the videobrush, and will paint with it the rectangle:
VideoBrush videoBrush = new VideoBrush();
videoBrush.Stretch = Stretch.Uniform;
videoBrush.SetSource(cs);
rectFill.Fill = videoBrush;
When running the application it will capture the video from the webcam and display it on screen:
Now for the capture of image. The handler of the “Capture Me!” button will enable asynchronous capturing of the image, and after getting the captured image will popup ChildWindow. The received image will be as a source for an image on this popup.
Button handler:
cs.AsyncCaptureImage(GetImage);
The “GetImage” function body (ScreenCapture is a ChildWindow instance):
ScreenCapture sc = new ScreenCapture();
sc.img.Source = bitmap;
sc.Show();
The running application:
The source for this app here (need Visual Studio 2010 Beta 2 & SL4 Beta).
Next time I’ll show how to use new built-in print support to print the captured image.
Stay tuned,
Alex