MULTIMEDIA

Silverlight : Capture a Webcam

10/9/2010 4:44:53 PM
Use Silverlight 4’s support for video capture devices.

To set this up, you need to first ask the user for permission to use the web cam. If granted, you can assign the device to a CaptureSource object and then assign that object to a VideoBrush. The VideoBrush can then be used as the Fill brush for the destination control (a Rectangle in this case).

This technique is demonstrated in Listings 1 and 2.

Listing 2. MainPage.xaml
<UserControl x:Class="WebCam.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/ markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<Grid x:Name="LayoutRoot" Background="White">
<Rectangle x:Name="videoRect" Fill="Bisque"
Width="640" Height="480"
VerticalAlignment="Top"
HorizontalAlignment="Left"/>
<Button Content="Start Webcam"
Name="buttonStartWebcam"
Width="98"
Height="23"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Click="buttonStartWebcam_Click" />
</Grid>
</UserControl>

Listing 2. MainPage.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace WebCam
{
public partial class MainPage : UserControl
{
CaptureSource _captureSource = null;

public MainPage()
{
InitializeComponent();

}

void StartWebcam()
{
if (_captureSource != null &&
_captureSource.State != CaptureState.Started)
{
if (CaptureDeviceConfiguration.AllowedDeviceAccess
|| CaptureDeviceConfiguration.RequestDeviceAccess())
{
VideoCaptureDevice device = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();
if (device != null)
{
_captureSource = new CaptureSource();
_captureSource.VideoCaptureDevice = device;
_captureSource.Start();
VideoBrush brush = new VideoBrush();
brush.Stretch = Stretch.Uniform;
brush.SetSource(_captureSource);
videoRect.Fill = brush;
}
}
}
}
private void buttonStartWebcam_Click(object sender,
RoutedEventArgs e)
{
StartWebcam();
}
}
}


Other  
  •  Silverlight : Make Your Application Run out of the Browser
  •  Silverlight : Put Content into a 3D Perspective
  •  Silverlight : Response to Timer Events on the UI Thread
  •  Silverlight : Build a Download and Playback Progress Bar
  •  Silverlight : Play a Video
  •  C# 4.0 : Add a Static Constructor and Initialization
  •  C# 4.0 : Add a Constructor
  •  .NET Compact Framework : Font Selection
  •  .NET Compact Framework : Drawing Text
  •  Programming the Service Bus
  •  WCF Services : Generics
  •  WCF Services : Delegates and Data Contracts
  •  WCF Services : Enumerations
  •  WCF Services : Versioning
  •  WCF Services : Data Contract - Equivalence
  •  WCF Services : Data Contract - Hierarchy
  •  WCF Services : Data Contract - Attributes
  •  Executing Work on a Background Thread with Updates
  •  Persisting Data on the Client with Silverlight 4
  •  Dynamically Loading XAML on Silverlight 4
  •  
    Top 10
    Most Favorite Business Softwares – Feb 2013
    Maingear Nomad 15 – Don’t Judge A Gaming Laptop By Its Cover
    MSI GX60 Gaming Notebook - Great Looks And A Fast GPU
    OCUK Limited Edition P170EM - A Great Screen And Balanced Setup
    Samsung Series 5 550P5C-S03 Review - A Perfect Farewell To Windows 7
    Kobo Mini eReader Review
    Keep Kids Online Safely (Part 3)
    Keep Kids Online Safely (Part 2)
    Keep Kids Online Safely (Part 1)
    Nikon 24-85MM F3.5-4.5G ED-IF VR With Amazing Optical Performance
    Most View
    Sony NEX-F3 Review (Part 3)
    Working with Access and Connectivity Policies in Vista
    GIGABYTE GA-Z77X-UP4 TH - Thunderbolt Technology Bonus
    Buffer Restrictions
    Mind Control (Part 1) - What this technology can offer
    The Sturdiest Smartphones At Present : Sony Xperia Go, Motorola Defy, Panasonic Eluga, Toshiba Regza T01-D
    Happy iMas (Part 1)
    Ditch Your Laptop For Your Phone (Part 2)
    Hands-On with the iPad Mini (Part 2)
    Ipad Lion (Part 3) - Other possible features
    Use a Stopwatch to Profile Your Code
    Programming the Mobile Web : Server-Side Browser Detection and Content Delivery - Content Adaptation
    Oracle Coherence 3.5 : Accessing the data grid (part 1) - Coherence console
    Philips Fidelio AS851 Docking Speaker for Android
    Fujifilm Releases X100S And X20
    Sharepoint 2010 : Monitoring SQL Server in a SharePoint Environment
    Android 4.0 Ice Cream Sandwich Guided Tour (Part 3)
    Sony Vaio T, ASUS Radeon HD7870 Directcu II Top
    Advanced ASP.NET : Data-Access Components (part 2) - Using the Data-Access Component
    Revel 1.5 - Do Pros Finally Have Their Panacea?