Click here to Skip to main content
15,867,915 members
Articles / Silverlight / Silverlight4

Silverlight 4 - Webcam Support

Rate me:
Please Sign up or sign in to vote.
4.80/5 (4 votes)
17 May 2010CPOL2 min read 17.7K   6   3
Webcam Support in Silverlight 4

Silverlight 4 is now not only more mature but also comes with rich features. Most of these features were recommendations from Silverlight community.

Webcam support is one of them. This was a very highly requested feature and has now been included in this version.

The following steps will get you up and running for the initial adventure:

Step 1

The first step is to get all the available capture devices (webcams, microphones, etc.) or default capture device on the system. So CaptureDeviceConfiguration class helps us to obtain information about all available devices. This helper class exposes a number of static members for this purpose.

  • CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices()
    It returns a collection of video devices on the system.
  • CaptureDeviceConfiguration.GetAvailableAudioCaptureDevices()
    All the audio devices.
  • CaptureDeviceConfiguration.GetDefaultAudioCaptureDevice()
    Default audio device.
  • CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice()
    Default video device.

So after the first step, we got the device

Step 2

Before any interaction with the device, Silverlight application needs permission from the user (the user must grant permission in the security prompt and that way, it ensures the call is safe).

  • CaptureDeviceConfiguration.RequestDeviceAccess(): To request access, call this static method in response to user initiated event like listbox item selection, button click, etc. If your application automates this call without user interaction (i.e., Load event), the method will return false and further action by application will throw an InvalidOperationException.

Okay, the application got the permission. Let's proceed with the next step.

Step 3

Now we have permission and the device handy. It's time for the application to interact with the device using CaptureSource class. This class allows you to collect the video feed from the camera. To make this happen,  create a CaptureSource object and set the VideoCaptureDevice to the selected device. With the help of CaptureSource, the application is now capable of performing the following task on the devices.

  • Start
  • Stop
  • State of the device
  • Capturing single video frame

Step 4

Now create a video brush. A VideoBrush gives us the ability to paint any area or controls with video content. At this stage, we use CaptureSource as a source of our brush.

Step 5

The next thing we need to do is to call CaptureSource.Start() method to begin capturing our live video.

Step 6

Finally, we'll set the Fill property of a Rectangle object to this brush.

Simple Application

Now we will code a very simple application:

  1. Create a Silverlight application and open the MainPage.xaml.
  2. Add one rectangle (for display purpose) and two buttons (start and stop):
    XML
    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel Name="mainStackpanel">
            <Rectangle Height="256" Name="myDisplay" Stroke="Black" 
                StrokeThickness="1" Width="584" />
            <StackPanel Name="subStackpanel" Orientation="Horizontal"
                HorizontalAlignment="Center">
                <Button x:Name="butStart" Content="Start" Click="butStart_Click"/>
                <Button x:Name="butStop" Content="Stop" Click="butStop_Click" />
            </StackPanel>
        </StackPanel>
    </Grid>
  3. Code behind as follows:
    C#
    public partial class MainPage : UserControl
      {
          CaptureSource captureSourec;
    
          public MainPage()
          {
              InitializeComponent();
          }
    
          private void butStart_Click(object sender, RoutedEventArgs e)
          {
              //Get the default video device
              VideoCaptureDevice myWebcam =
                  CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();
    
              //Grant permission
              if (CaptureDeviceConfiguration.AllowedDeviceAccess ||
                  CaptureDeviceConfiguration.RequestDeviceAccess())
              {
                  captureSourec = new CaptureSource();
                  captureSourec.VideoCaptureDevice  = myWebcam;
    
                  //Create video brush that will get feed from captureSourec
                  VideoBrush myBrush = new VideoBrush();
                  myBrush.SetSource(captureSourec);
                  myBrush.Stretch = Stretch.UniformToFill;
    
    
                  //Stat the web cam
                  captureSourec.Start();
    
                  //Now display through the Rectangle
                  myDisplay.Fill = myBrush;
              }
          }
    
          private void butStop_Click(object sender, RoutedEventArgs e)
          {
              if (captureSourec != null)
              {
                  captureSourec.Stop();
              }
          }
      }
    

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Lavender
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionImage not viewing Pin
Noman Sadiq15-Aug-14 23:59
Noman Sadiq15-Aug-14 23:59 
QuestionThe program stuck at waiting for the webcam premission Pin
Ba Love16-Oct-13 12:37
Ba Love16-Oct-13 12:37 
QuestionHow to encode image Pin
latomcus4-Aug-11 7:43
latomcus4-Aug-11 7:43 
Thank you. Very useful. Moving beyond user interface and capturing images is how do I store them in usable format. I searched various sites and only found one so far www.glo6.com[^]
Here is direct link: www.glo6.com/camera1.aspx. Is there a similar support from Microsoft for encoding images and perhaps video?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.