Taking a WebCam Photo with Silverlight

Silverlight Logo

WebCam and Microphone support was one of the most requested features for Silverlight and Microsoft has answered the call. There are several ways to interact with the WebCam, but i'll start with the most simple.  I've written a simple demo that you can download here that will let you start the WebCam, take a snapshot from the WebCam and save it out as an image:

The first class to be concerned with is the CaptureSource class. This class allows you to collect the video feed from the camera and use it with a VideoBrush to paint it on some surface. First let's figure out how to actually enable the camera. To start the webcam, you must request permission. You can do this with the CaptureDeviceConfiguration class. You can call the class' RequestDeviceAccess to ask the user to give you permission.  In addition, you can ask the class whether access has already been given by calling AllowedDeviceAccess property.  Surrounding the starting of the webcam is usually how thats handled like below:

CaptureSource src = new CaptureSource();

void cameraButton_Click(object sender, RoutedEventArgs e)
{
  if (CaptureDeviceConfiguration.AllowedDeviceAccess ||
    CaptureDeviceConfiguration.RequestDeviceAccess())
  {
    src.Start();
  }
}

By default, the CaptureSource uses the first found device, but you can also use the CaptureDeviceConfiguration class to iterate through the devices and attach the right one to the CaptureSource as well.

While you would normally use the VideoSink and AudioSink classes to actually capture actual samples, you can also use the CaptureSource class to capture individual frames of the webcam. A future example will delve into the sink classes, but in this example lets grab an individual frame. In fact, we can use the CaptureSource to make the webcam act like a camera.  The CaptureSource class has an AsyncCaptureImage method that presents a callback to retrieve a WriteableBitmap of the capture source. In this example I am taking the bitmap and showing it on an image control:

src.AsyncCaptureImage(bmp =>
  {
    snapshot.Source = bmp;
    src.Stop();
  });

You can download the sample here:

http://wildermuth.com/downloads/takingmypicture.zip

Comments:

Gravatar

Your link seems to be broken :(

Gravatar

Hi Shawn

I just came accross your post here, but the download link doesn't work.

BTW, have you seen my blog post where I save the image as an jpeg file? I wrote it on sunday and published it yesterday:
http://kodierer.blogspot.com/2009/11/edgecam-shots-saving-silverlight-4.html

"Great minds think alike." :D

Gravatar

Link Fixed...sorry...

Gravatar

very good, thank u

Gravatar

hey Shawn ,i m having little bit problem with this can help me , i m using vs 2008 and no idea about silverlight can u show mw other way so i can acquire image from wab cam and store it to particular location in database
if u want i can give u more detail about my requirment

thank u in advance for u r help

Gravatar

Kaushk,

Two things:

- This is Silverlight 4 (which requires Visual Studio 2010 Beta 2).
- I can't create individual examples for everyone who asks, I just don't have the time.

Move to SL4 and try it again.

Gravatar

Thanks and nice work!
But where did you get Silver Light 4 Beta.
I just installed Visual Studio 2010 Beta 2.
No trace of Silver Light 4.
Sebastine

Gravatar

Hey Shawn, big fan of your books. Im curious is it possible to stream webcam with Silverlight or is the functionally limited images?

Gravatar

Seth,

Yes, look at VideoSink and AudioSink, they are the key to making that work.

Gravatar

Hey Shawn,how i can use it with vs 2008 and framework 3.5

Gravatar

Thank you very much shawn....
This example has helped me a lot......

Gravatar

Hi Shawn how can I get silverlight-4 when I tried to download that It says "coming soon!" I already have silverlight-3.

Gravatar

Faisal,

You can't. Its Silverlight 4 + VS2010 only.

Gravatar

Sarah,

You must install the SDK or Silverlight Tools. There is no runtime yet (it will be released in Mid April). Find the tools here:

http://www.silverlight.net/getstarted/silverlight-4/


 



 
Save Cancel