SIGN UP MEMBER LOGIN:    
ARTICLE

Accessing webcam in Silverlight application

Posted by Praveen Kumar Articles | Silverlight with C# April 22, 2010
In this article, we will see how to access the webcam from silverlight application.
Reader Level:
Download Files:
 

In this article, we will see how to access the webcam from silverlight application.

My XAML code will look like below

<UserControl x:Class="SilverlightApplication4.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" xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" xmlns:riaControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.DomainServices" Height="427" Width="497">
    <Grid x:Name="LayoutRoot" Background="White" Height="413" Width="482">
        <Rectangle RadiusX="5" RadiusY="5" x:Name="camview" Height="249" HorizontalAlignment="Left" Margin="75,40,0,0" Stroke="Black" StrokeThickness="1" VerticalAlignment="Top" Width="337" Fill="Black" />
        <Button Content="Start Cam" Height="23" HorizontalAlignment="Left" Margin="127,330,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click_1" />
        <Button Content="Stop Cam" Height="23" HorizontalAlignment="Left" Margin="248,330,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />
    </Grid>
</UserControl>

Here I have taken one rectangle and two buttons inside the the grid.

First button will start the cam to capture the image and second is used to stop the cam.

"Start Cam" button code is as follows:

private void button1_Click_1(object sender, RoutedEventArgs e)
{
    if (!CaptureDeviceConfiguration.AllowedDeviceAccess)
    {
        if (!CaptureDeviceConfiguration.RequestDeviceAccess())
        {
            return;
        }
    }
    StartWebCam();
}

Here we are accessing the webcam and if we have permission to do so then we are calling method "StartWebCam()".

private void StartWebCam()
{
    cs = new CaptureSource();
    cs.VideoCaptureDevice = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();
    VideoBrush PreviewBrush = new VideoBrush();
    PreviewBrush.SetSource(cs);
    camview.Fill = PreviewBrush;
    cs.Start();
}

We are capturing video through the default capture device found in the system. Then we fill the rectangle with the video we obtained from webcam.

Output:

1.gif 

When we click "Start Cam" button

2.gif

Next step: Let's see in future how to capture image from webcam and display it in a webpage.

Summary:

We have just seen a simple method to access the webcam from our silverlight application.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor