Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Gauge for SharePoint
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Gauge for SharePoint
Search :       Advanced Search »
Home » Silverlight » Accessing webcam in Silverlight application

Accessing webcam in Silverlight application

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

Author Rank :
Page Views : 7820
Downloads : 324
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
SilverlightApplication4.zip
 
 
Nevron Gauge for SharePoint
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


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.

Now that we are able to access the cam and got the picture from webcam we are moving to next step as to capture the image.

For this I have added another button with caption "Capture" and a rectangle control.
 
Code for capture button is as follows:
 
private void button3_Click(object sender, RoutedEventArgs e)
{
    cs.CaptureImageCompleted += new EventHandler<CaptureImageCompletedEventArgs>(cs_CaptureImageCompleted);
    cs.CaptureImageAsync();
}
 
void cs_CaptureImageCompleted(object sender, CaptureImageCompletedEventArgs e)
{
    ImageBrush imagebrush = new ImageBrush();
    imagebrush.ImageSource = e.Result;
    rectangle1.Fill = imagebrush;
}
 
In this above code, once we are done with image capture we are displaying it in a rectangle through ImageBrush.
 
Screen before capture:
 
capture1.gif 
 
Screen after image capture
 
capture2.gif

 

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Author
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
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.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Gauge for SharePoint
Become a Sponsor
 Comments
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.