Click here to Skip to main content
11,505,486 members (65,884 online)
Click here to Skip to main content

WPF: Webcam Control

, 18 Nov 2011 CPOL 337.9K 55.7K 294
Rate this:
Please Sign up or sign in to vote.
A WPF UserControl for displaying and recording video from a webcam
This is an old version of the currently published article.

Screenshot_1.png

Introduction

I have on several occasions scoured the net for a simple to use WPF webcam control and either my search queries were awful or I just wasn't comfortable with whatever I found. This webcam interest was recently increased when I read an article, here on CodeProject, that referred to an application that made use of a webcam. It was a Silverlight article and the ease with which one could utilize a webcam in Silverlight made me envious of WPF's little brother (or is it sister?). The VideoBrush in Silverlight is especially a nice touch.

It is with this pain and envy in mind that I decided to try my hand at creating a WPF control that could;

  1. Display webcam video with little coding effort,
  2. Allow saving of webcam video to harddisk, with little coding effort,
  3. Save the webcam video in a variety of video formats.

Background

With the previously mentioned goals in mind I created a WPF UserControl that has the following features;

  • Displays webcam video,
  • Enables saving of webcam videos to harddisk,
  • Enables saving of webcam videos in either; .wmv, .mp4, or .flv format.

The Webcam control makes heavy use of the Expression Encoder 4 SDK. You therefore need to have the SDK installed on your machine to make use of the UserControl. You can download the SDK from here.

Requirements

To make use of the Webcam control you require;

  • .NET Framework 4.0,
  • Expression Encoder SDK

Using the Webcam Control

To use the Webcam control in your WPF application add a reference to WebcamControl.dll and a using/imports statement for WebcamControl at the top of your class.

Add a reference to Microsoft.Expression.Encoder.dll . Do this by using the Add Reference dialog box, selecting the .NET tab, and selecting Microsoft.Expression.Encoder from the listbox. The Expression Encoder assemblies should be available if you have installed Expression Encoder 4.

The following example, which is the downloadable sample application, shows the use of the Webcam control. The sample application contains; a ContentControl that will host the Webcam control, two ComboBoxes for displaying the names of audio and video devices connected to the machine, and four buttons.

Imports Microsoft.Expression.Encoder.Devices
Imports WebcamControl

Class MainWindow
    Dim webCamCtrl As New Webcam

    Private Sub MainWindow_Loaded(ByVal sender As Object, _
                                  ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        FindDevices()

        Dim path As String = "C:\VideoClips"

        If Directory.Exists(path) = False Then
            Directory.CreateDirectory(path)
        End If

        ' Set some properties of the Webcam control.
        webCamCtrl.VideoDirectory = path
        webCamCtrl.VidFormat = VideoFormat.wmv

        ' Set the Webcam control as the ContentControl's content.
        ContentControl1.Content = webCamCtrl

        VidDvcsComboBox.SelectedIndex = 0
        AudDvcsComboBox.SelectedIndex = 0
    End Sub

    ' Find available a/v devices.
    Private Sub FindDevices()
        Dim vidDevices = EncoderDevices.FindDevices(EncoderDeviceType.Video)
        Dim audDevices = EncoderDevices.FindDevices(EncoderDeviceType.Audio)

        For Each dvc In vidDevices
            VidDvcsComboBox.Items.Add(dvc.Name)
        Next

        For Each dvc In audDevices
            AudDvcsComboBox.Items.Add(dvc.Name)
        Next
    End Sub

    Private Sub StartButton_Click(ByVal sender As System.Object, _
                                  ByVal e As System.Windows.RoutedEventArgs) Handles StartButton.Click

        ' Display webcam images on control.
        Try
            webCamCtrl.StartCapture()
        Catch ex As Microsoft.Expression.Encoder.SystemErrorException
            MessageBox.Show("Device is in use by another application")
        End Try
    End Sub

    Private Sub EndButton_Click(ByVal sender As Object, _
                                ByVal e As System.Windows.RoutedEventArgs) Handles EndButton.Click

        ' Stop the display of webcam video.
        webCamCtrl.StopCapture()
    End Sub

    Private Sub RecordButton_Click(ByVal sender As System.Object, _
                                   ByVal e As System.Windows.RoutedEventArgs) Handles RecordButton.Click

        ' Start recording of webcam video to harddisk.
        webCamCtrl.StartRecording()
    End Sub

    Private Sub StopRecordButton_Click(ByVal sender As Object, _
                                       ByVal e As System.Windows.RoutedEventArgs) Handles StopRecordButton.Click

        ' Stop recording of webcam video to harddisk.
        webCamCtrl.StopRecording()
    End Sub

    Private Sub VidDvcsComboBox_SelectionChanged(ByVal sender As Object, _
                                                 ByVal e As System.Windows.Controls.SelectionChangedEventArgs) _
                                             Handles VidDvcsComboBox.SelectionChanged

        ' Set which video device to use.
        webCamCtrl.VideoDevice = VidDvcsComboBox.SelectedValue
    End Sub
   
    Private Sub AudDvcsComboBox_SelectionChanged(ByVal sender As Object, _
                                                 ByVal e As System.Windows.Controls.SelectionChangedEventArgs) _
                                             Handles AudDvcsComboBox.SelectionChanged

        ' Set which audio device to use.
        webCamCtrl.AudioDevice = AudDvcsComboBox.SelectedValue
    End Sub
End Class

As you can see from the sample code using the Webcam control is not a hard affair once you have the necessary references and imports/using statements.

NB: The VideoDevice and AudioDevice properties of Webcam control are dependency properties and can therefore be data-bound to the SelectedValue property of the necessary ComboBox.

Webcam

The following are the members of interest in class Webcam;

Properties

  Name Description Type
PropertyIcon.png VideoDirectory Gets or Sets the folder where the recorded webcam video will be saved. This is a dependency property. String
PropertyIcon.png VidFormat Gets or Sets the video format in which the webcam video will be saved. This is a dependency property. (The default format is .wmv) VideoFormat
PropertyIcon.png VideoDevice Gets or Sets the name of the video device to be used. This is a dependency property. String
PropertyIcon.png AudioDevice Gets or Sets the name of the audio device to be used. This is a dependency property. String
PropertyIcon.png IsRecording Gets a value indicating whether video recording is taking place. This is a read-only property. Boolean

Methods

  Name Description
MethodIcon.png StartCapture Displays webcam video on control. (Throws a Microsoft.Expression.Encoder.SystemErrorException if a specified device is already in use by another application)
MethodIcon.png StopCapture Stops the capturing/display of webcam video. (Stops any current recording of webcam video)
MethodIcon.png StartRecording Starts the recording of webcam video to a video file. (Throws a DirectoryNotFoundException if the directory specified in the VideoDirectory property is not found)
MethodIcon.png StopRecording Stops the recording of webcam video.

The Code

The XAML markup for the UserControl is;

<UserControl x:Class="Webcam" 
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             Height="Auto" Width="Auto" MinHeight="100" MinWidth="100" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
             mc:Ignorable="d" d:DesignWidth="320" d:DesignHeight="240" Name="Webcam">
    <Grid>
        <WindowsFormsHost Margin="0,0,0,0" Name="WinFormHost" Background="{x:Null}">
            <wf:Panel x:Name="WebcamPanel" Size="320,240" />
        </WindowsFormsHost>
    </Grid>
</UserControl>

The dimensions of the WinForm Panel are adjusted when the control is loaded so that the webcam video will occupy the entire area of the Panel's parent element.

    Private Sub Webcam_Loaded(ByVal sender As Object, _
                              ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        ' Set dimensions of WinForm Panel to current dimensions
        ' of the UserControl.
        Dim panelWidth As Integer = CInt(Me.ActualWidth)
        Dim panelHeight As Integer = CInt(Me.ActualHeight)

        WebcamPanel.Width = panelWidth
        WebcamPanel.Height = panelHeight
    End Sub

As highlighted earlier, Webcam contains several properties and methods that aid in its operation. The VideoDevice dependency property is defined as follows;

    ''' <summary>
    ''' Gets or Sets the name of the video device to be used.
    ''' </summary>    
    Public Property VideoDevice() As String
        Get
            Return CType(GetValue(VideoDeviceProperty), String)
        End Get
        Set(ByVal value As String)
            SetValue(VideoDeviceProperty, value)
        End Set
    End Property

    Public Shared VideoDeviceProperty As DependencyProperty = _
        DependencyProperty.Register("VideoDevice", GetType(String), GetType(Webcam), _
                                    New FrameworkPropertyMetadata(New PropertyChangedCallback( _
                                                                  AddressOf VidDeviceChange)))

    Private Shared Sub VidDeviceChange(ByVal source As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
        Dim deviceName As String = CType(e.NewValue, String)
        Dim eDev = EncoderDevices.FindDevices(EncoderDeviceType.Video).Where _
                   (Function(dv) dv.Name = deviceName)

        If (eDev.Count > 0) Then
            CType(source, Webcam).vidDevice = eDev.First
            CType(source, Webcam).Display()
        End If
    End Sub

The StartCapture method displays the webcam video in the WinForm Panel, if the necessary properties are set;

    ''' <summary>
    ''' Display webcam video on control.
    ''' </summary>
    Public Sub StartCapture()
        If (canCapture = False) Then
            canCapture = True
            Try
                Display()
            Catch ex As Microsoft.Expression.Encoder.SystemErrorException
                canCapture = False
                Throw New Microsoft.Expression.Encoder.SystemErrorException
            End Try
        Else
            Exit Sub
        End If
    End Sub
    
    ' Display video from webcam.
    Private Sub Display()
        If (canCapture = True) Then
            If (vidDevice IsNot Nothing) Then
                StopRecording()
                Dispose()

                job = New LiveJob

                deviceSource = job.AddDeviceSource(vidDevice, audDevice)
                deviceSource.PreviewWindow = New PreviewWindow(New HandleRef(WebcamPanel, WebcamPanel.Handle))

                job.ActivateSource(deviceSource)
            End If
        End If
    End Sub

The StartRecording method records video from the webcam to the harddisk;

    ''' <summary>
    ''' Starts the recording of webcam video to a video file.
    ''' </summary>
    Public Sub StartRecording()
        If (dir <> String.Empty AndAlso job IsNot Nothing) Then
            If (Directory.Exists(dir) = False) Then
                Throw New DirectoryNotFoundException("The specified directory does not exist")
                Exit Sub
            End If

            ' If canCapture is true then it means the control is capturing video 
            ' from the webcam.
            If (canCapture = True) Then
                StopRecording()
                job.PublishFormats.Clear()

                Dim timeStamp As String = DateTime.Now.ToString
                timeStamp = timeStamp.Replace("/", "-")
                timeStamp = timeStamp.Replace(":", ".")
                Dim filePath As String = dir & "\WebcamVid " & timeStamp & "." & format

                Dim fileArchFormat As New FileArchivePublishFormat(filePath)
                job.PublishFormats.Add(fileArchFormat)
                job.StartEncoding()

                _isRecording = True
            End If
        End If
    End Sub

The VideoFormat enumeration contains three members;

Public Enum VideoFormat
    wmv
    mp4
    flv
End Enum

You can take a look at the other properties and methods defined in class Webcam by downloading the src files from the download link at the top of this article.

Conclusion

I hope that you picked up something useful from this article. I'm a novice in audio-video-encoding-decoding matters so if you have any questions regarding such technicalities please try to post them in the associated forums here on CodeProject. Suggestions will be beneficial, as well as answers you receive to any technical queries that may be associated with this article's content. Thanks.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Meshack Musundi
Software Developer
Kenya Kenya
Meshack is an avid programmer with a bias towards WPF and VB.NET. He has about 5 years of programming experience initially starting off with Java before shifting to .NET, thanks to the allure of WPF. He currently resides in a small town in Kiambu county, Kenya.

Awards;
  • CodeProject MVP 2013
  • CodeProject MVP 2012
  • Best VB.NET article of January 2015
  • Best VB.NET article of August 2013
  • Best VB.NET article of February 2013
  • Best VB.NET article of October 2012
  • Best VB.NET article of July 2012
  • Best VB.NET article of February 2012
  • Best VB.NET article of January 2012
  • Best VB.NET article of November 2011
  • Best VB.NET article of June 2011
  • Best VB.NET article of May 2011
  • Best VB.NET article of March 2011
  • Best VB.NET article of February 2011
  • Best VB.NET article of January 2011
  • Best VB.NET article of December 2010
  • Best VB.NET article of November 2010

Comments and Discussions


Discussions posted for the Published version of this article. Posting a message here will take you to the publicly available article in order to continue your conversation in public.
 
QuestionWhy Windows Forms Panel Pin
Joel Palmer16-Apr-15 19:44
memberJoel Palmer16-Apr-15 19:44 
AnswerRe: Why Windows Forms Panel Pin
Meshack Musundi16-Apr-15 23:24
professionalMeshack Musundi16-Apr-15 23:24 
Questionhow to have a XAP from this project ? Pin
amr ismail30-Mar-15 4:23
memberamr ismail30-Mar-15 4:23 
QuestionVideo Director Pin
bruno.picardi27-Mar-15 21:35
memberbruno.picardi27-Mar-15 21:35 
GeneralRe: Video Director Pin
Meshack Musundi28-Mar-15 1:16
professionalMeshack Musundi28-Mar-15 1:16 
GeneralRe: Video Director Pin
bruno.picardi28-Mar-15 1:37
memberbruno.picardi28-Mar-15 1:37 
QuestionSave the Video Pin
Member 879873427-Mar-15 19:05
memberMember 879873427-Mar-15 19:05 
AnswerRe: Save the Video Pin
Meshack Musundi28-Mar-15 1:20
professionalMeshack Musundi28-Mar-15 1:20 
Questionvideo recording using WPF in C# Pin
Bushra Sadia16-Mar-15 0:05
memberBushra Sadia16-Mar-15 0:05 
GeneralRe: video recording using WPF in C# Pin
Meshack Musundi20-Mar-15 0:07
professionalMeshack Musundi20-Mar-15 0:07 
QuestionHow to video processing and manipulate in real-time? Pin
hzawary30-Jan-15 4:15
memberhzawary30-Jan-15 4:15 
QuestionHow I can Seek Video File ? Pin
Bình Toác28-Jan-15 20:47
memberBình Toác28-Jan-15 20:47 
QuestionProblem porting to x86 tablet Pin
Jon 12326-Jan-15 5:14
memberJon 12326-Jan-15 5:14 
AnswerRe: Problem porting to x86 tablet Pin
Meshack Musundi26-Jan-15 8:14
professionalMeshack Musundi26-Jan-15 8:14 
GeneralRe: Problem porting to x86 tablet Pin
Jon 12326-Jan-15 22:21
memberJon 12326-Jan-15 22:21 
GeneralRe: Problem porting to x86 tablet Pin
Jon 12326-Jan-15 23:03
memberJon 12326-Jan-15 23:03 
Questionhow to add VB code in wpf Pin
deepika wani18-Jan-15 22:55
memberdeepika wani18-Jan-15 22:55 
AnswerRe: how to add VB code in wpf Pin
Meshack Musundi19-Jan-15 3:35
professionalMeshack Musundi19-Jan-15 3:35 
QuestionRe: how to add VB code in wpf Pin
deepika wani19-Jan-15 18:29
memberdeepika wani19-Jan-15 18:29 
AnswerRe: how to add VB code in wpf Pin
Meshack Musundi20-Jan-15 2:17
professionalMeshack Musundi20-Jan-15 2:17 
GeneralRe: how to add VB code in wpf Pin
deepika wani20-Jan-15 19:28
memberdeepika wani20-Jan-15 19:28 
GeneralRe: how to add VB code in wpf Pin
amr ismail30-Mar-15 4:38
memberamr ismail30-Mar-15 4:38 
AnswerRe: how to add VB code in wpf Pin
amr ismail30-Mar-15 4:57
memberamr ismail30-Mar-15 4:57 
Bug32bit only DLL ? Pin
tlhintoq16-Jan-15 5:33
membertlhintoq16-Jan-15 5:33 
GeneralRe: 32bit only DLL ? Pin
Meshack Musundi16-Jan-15 8:58
professionalMeshack Musundi16-Jan-15 8:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 18 Nov 2011
Article Copyright 2011 by Meshack Musundi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid