Example Code for Using Webcams in Silverlight 4

XAML

<UserControl x:Class="WebcamCapture.MainPage"
   xmlns=https://schemas.microsoft.com/winfx/2006/xaml/presentation
   xmlns:x=https://schemas.microsoft.com/winfx/2006/xaml
   xmlns:d=https://schemas.microsoft.com/expression/blend/2008
   xmlns:mc=https://schemas.openxmlformats.org/markup-compatibility/2006
   mc:Ignorable="d"
   Height="400" Width="800"
   d:DesignHeight="400" d:DesignWidth="800" Loaded="UserControl_Loaded">

<!-- Style for the buttons -->
   <UserControl.Resources>
      <Style x:Name="ButtonStyle" TargetType="Button">
         <Setter Property="Height" Value="50" />
         <Setter Property="Width" Value="100" />
         <Setter Property="Margin" Value="5" />
      </Style>
   </UserControl.Resources>

   <Grid x:Name="LayoutRoot" Background="LightBlue">
      <Grid.ColumnDefinitions>
         <ColumnDefinition />
         <ColumnDefinition />
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
         <RowDefinition Height="100"/>
         <RowDefinition Height="*"/>
      </Grid.RowDefinitions>

      <!-- StackPanel to hold the Buttons -->
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Center"
Grid.ColumnSpan="2" 
                  Grid.Row="0">
         <Button Style="{StaticResource ButtonStyle}" 
x:Name="StartButton" Click="StartButton_Click"
                 Content="Start Camera" />
         <Button Style="{StaticResource ButtonStyle}"
x:Name="StopButton" Click="StopButton_Click" 
                 Content="Stop Camera" />
         <Button Style="{StaticResource ButtonStyle}"
x:Name="CaptureButton" Click="CaptureButton_Click"
Content="Capture Image" />
      </StackPanel>

      <!-- The Fill property will be set to the webCam VideoBrush-->
      <Rectangle x:Name="webcamDisplay" Grid.Row="1" Grid.Column="0" Margin="5"/>

      <!-- The Fill property will be set to the capturedImage ImageBrush-->
      <Rectangle x:Name="capturedDisplay" Grid.Row="1" Grid.Column="1" Margin="5" />

   </Grid>
</UserControl>

 

 C#

namespace

WebcamCapture
{
public partial class MainPage : UserControl
   {
      public MainPage()
{
InitializeComponent();
}

      // the main object for interacting with the audio/video devices
      public CaptureSource captureSource;

      // the webcam device
      public VideoCaptureDevice webcam;

      // brush for the video feed
      public VideoBrush webcamBrush;

      // brush for the captured video frame
      public ImageBrush capturedImage;

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
captureSource = new CaptureSource();
        
// async capture failed event handler
         captureSource.CaptureFailed +=
             new EventHandler<ExceptionRoutedEventArgs>(CaptureSource_CaptureFailed);

// async capture completed event handler
         captureSource.CaptureImageCompleted +=
             new EventHandler<CaptureImageCompletedEventArgs>(CaptureSource_CaptureImageCompleted);

         // get the webcam. null is returned if there is not a webcam installed
         webcam = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();

if (null != webcam)
{
            // set the video capture source to the WebCam
            captureSource.VideoCaptureDevice = webcam;

// set the souce on the VideoBrush used to display the video
            webcamBrush = new VideoBrush();
webcamBrush.SetSource(captureSource);

// set the Fill property of the Rectangle (defined in XAML) to the VideoBrush
            webcamDisplay.Fill = webcamBrush;

// the brush used to fill the display rectangle
capturedImage = new ImageBrush();

            // set the Fill property of the Rectangle (defined in XAML) to the ImageBrush
capturedDisplay.Fill = capturedImage;
}
}

private void StartButton_Click(object sender, RoutedEventArgs e)
{
            // request access to the device and verify the VideoCaptureDevice is not null
if(CaptureDeviceConfiguration.RequestDeviceAccess() &&
captureSource.VideoCaptureDevice != null )
{
captureSource.Start();
}
}

private void StopButton_Click(object sender, RoutedEventArgs e)
{
         // verify the VideoCaptureDevice is not null
         if (captureSource.VideoCaptureDevice != null)
{
captureSource.Stop();
}
}

private void CaptureButton_Click(object sender, RoutedEventArgs e)
{
         // verify the VideoCaptureDevice is not null and the device is started
         if (captureSource.VideoCaptureDevice != null &&
              captureSource.State == CaptureState.Started)
{
captureSource.CaptureImageAsync();
}
}

void CaptureSource_CaptureImageCompleted(object sender, CaptureImageCompletedEventArgs e)
{
         // Set the ImageBrush source to the WriteableBitmap passed in through the event arguments
         capturedImage.ImageSource = e.Result;
}

void CaptureSource_CaptureFailed(object sender, ExceptionRoutedEventArgs e)
{
         // For the sake of this example, simply show a messagebox
         MessageBox.Show(string.Format("Failed to capture image: {0}", e.ErrorException.Message));
}
}
}