Click here to Skip to main content
15,885,757 members
Articles / Programming Languages / C# 4.0

Capture Image from Webcam using Silverlight

Rate me:
Please Sign up or sign in to vote.
4.19/5 (9 votes)
8 Nov 2012CPOL5 min read 56.4K   4.6K   17  
Capture a frame from live webcam feed and save it as an image file to disk
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.IO;
using System.Configuration;
using System.ServiceModel.DomainServices.Client;
using System.ComponentModel.DataAnnotations;
using ImageTools.Helpers;
using ImageTools.IO.Png;
using ImageTools.IO.Jpeg;
using ImageTools.Controls;

namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        ImageBrush ib;
        CaptureSource source;
        private WriteableBitmap _capimage;
        public WriteableBitmap CapturedImage
        {
            get 
            {              
                return _capimage; 
            }
            set { _capimage = value; }
        }

        public MainPage()
        {
            InitializeComponent();
        }

        void source_CaptureImageCompleted(object sender, CaptureImageCompletedEventArgs e)
        {
            ib.ImageSource = e.Result;
            CapturedImage = e.Result;                    
        }       

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "PNG files (*.PNG)|*.png|All Files (*.*)|*.*";
            var enc = new PngEncoder();
            if ((bool)sfd.ShowDialog())
            {
                Stream stream = sfd.OpenFile();
                var image = Imager.ToImg(CapturedImage);
                enc.Encode(image, stream);
                stream.Close();
            }
        }

        private void btnActivate_Click(object sender, RoutedEventArgs e)
        {
            source = new CaptureSource();
            VideoCaptureDevice vcd = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();
            source.VideoCaptureDevice = vcd;
            VideoBrush vb = new VideoBrush();
            ib = new ImageBrush();
            vb.SetSource(source);
            rectangle1.Fill = vb;
            rectangle2.Fill = ib;

            if (CaptureDeviceConfiguration.RequestDeviceAccess())
            {
                source.Start();
            }
        }

        private void btnFreeze_Click(object sender, RoutedEventArgs e)
        {
            source.CaptureImageCompleted += new EventHandler<CaptureImageCompletedEventArgs>(source_CaptureImageCompleted);
            source.CaptureImageAsync();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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)
United Kingdom United Kingdom
A software developer in pursuit of craftsmanship and the zen of software.

Comments and Discussions