Click here to Skip to main content
15,860,943 members
Articles / Desktop Programming / WPF

WPF: Webcam Control

Rate me:
Please Sign up or sign in to vote.
4.92/5 (150 votes)
25 Sep 2017CPOL3 min read 1.3M   90.2K   369   526
A WPF control for displaying and recording video from a webcam

WPF_WebcamControl/Screenshot_1.png

Introduction

WebcamControl is a WPF control for displaying and recording webcam videos using the Expression Encoder SDK. It also enables taking of webcam video snapshots.

Features

The control has the following features,

  • Displays webcam video,
  • Enables saving of webcam video (in .wmv format),
  • Enables saving of video snapshots.

Requirements

NB: To use the control ensure that your project's build platform is set to x86.

Webcam

You can install the control via NuGet by running the following command in the NuGet Package Manager console,

Install-Package WpfWebcamControl -Version 3.3.1

Properties

 NameDescription
WPF_WebcamControl/Property.pngVideoFileFormatGets the format in which webcam videos will be saved. This is a dependency property of type String.
WPF_WebcamControl/Property.pngSnapshotFormatGets or sets the format used when saving snapshots of webcam preview. This is a dependency property of type ImageFormat.
WPF_WebcamControl/Property.pngVideoDeviceGets or sets the webcam to be used. This is a dependency property of type Microsoft.Expression.Encoder.Devices.EncoderDevice.
WPF_WebcamControl/Property.pngAudioDeviceGets or sets the microphone to be used. This is a dependency property of type Microsoft.Expression.Encoder.Devices.EncoderDevice
WPF_WebcamControl/Property.pngVideoNameGets or sets the name of the video file – which should not include the file extension. This is a dependency property of type String.
WPF_WebcamControl/Property.pngVideoDirectoryGets or sets the folder where the webcam video will be saved. This is a dependency property of type String.
WPF_WebcamControl/Property.pngImageDirectoryGets or sets the folder where video snapshots will be saved. This is a dependency property of type String.
WPF_WebcamControl/Property.pngBitrateGets or sets the bitrate. This is a dependency property of type Integer. (The default value is 2000).
WPF_WebcamControl/Property.pngFrameRateGets or sets the frame rate, in frames per second. This is a dependency property of type Integer. (The default value is 15).
WPF_WebcamControl/Property.pngFrameSizeGets or sets the size of the video profile. This is a dependency property of type System.Drawing.Size. (The default value is 320x240).
WPF_WebcamControl/Property.pngIsRecordingGets a value indicating whether video recording is taking place. This is a dependency property of type Boolean.

Methods

 NameDescription
WPF_WebcamControl/Method.pngStartPreviewStarts the display of the webcam preview. (Throws a Microsoft.Expression.Encoder.SystemErrorException if a specified device is already in use by another application)
WPF_WebcamControl/Method.pngStopPreviewStops the display of the webcam preview and also stops any ongoing recording.
WPF_WebcamControl/Method.pngStartRecordingStarts recording of webcam video and returns the full path of the video file.
WPF_WebcamControl/Method.pngStopRecordingStops the recording of webcam video.
WPF_WebcamControl/Method.pngTakeSnapshotSaves a snapshot of webcam preview and returns the full path of the image file.

Example

The following example shows how to use the control. The example contains a Webcam control, two combo boxes for listing video and audio devices, and buttons for calling the various control functions.

XML
<Window x:Class="WPF_Webcam_CS.MainWindow"
        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"
        xmlns:local="clr-namespace:WPF_Webcam_CS"
        xmlns:cam="clr-namespace:WebcamControl;assembly=WebcamControl"
        Title="WPF Webcam" Height="495" Width="353">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="75"/>
            <RowDefinition Height="132"/>
        </Grid.RowDefinitions>

        <cam:Webcam x:Name="WebcamViewer" Margin="10"
                    FrameRate="30"
                    FrameSize="640, 480"                    
                    ImageDirectory="C:\WebcamSnapshots"
                    VideoDirectory="C:\VideoClips"
                    VideoDevice="{Binding SelectedItem, ElementName=VidDevices}"
                    AudioDevice="{Binding SelectedItem, ElementName=AudDevices}"/>

        <Grid Grid.Row="1" HorizontalAlignment="Center">
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>

            <TextBlock Text="Video Device" VerticalAlignment="Center"/>
            <ComboBox x:Name="VidDevices" Grid.Column="1" Margin="10,0,0,0"
                      Width="210" Height="24"
                      ItemsSource="{Binding VideoDevices}"
                      DisplayMemberPath="Name"
                      SelectedIndex="0"/>
            
            <TextBlock Text="Audio Device" Grid.Row="1" VerticalAlignment="Center"/>
            <ComboBox x:Name="AudDevices" Grid.Row="1" Grid.Column="1"
                      Width="210" Height="24" Margin="10,0,0,0"
                      ItemsSource="{Binding AudioDevices}"
                      DisplayMemberPath="Name"
                      SelectedIndex="0"/>            
        </Grid>

        <Grid Grid.Row="2" HorizontalAlignment="Center" Margin="0,10">
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Button Content="Start Capture" 
                    Height="24" Width="112" HorizontalAlignment="Right" Margin="0,0,10,0"
                    Click="StartCaptureButton_Click"/>
            
            <Button Grid.Column="1" Content="Stop Capture"
                    Height="24" Width="112" HorizontalAlignment="Left" Margin="10,0,0,0"  
                    Click="StopCaptureButton_Click"/>

            <Button Grid.Row="1" Content="Start Recording"
                    Height="24" Width="112" HorizontalAlignment="Right" Margin="0,0,10,0"
                    Click="StartRecordingButton_Click"/>

            <Button Grid.Row="1" Grid.Column="1" Content="Stop Recording" 
                    Height="24" Width="115" HorizontalAlignment="Left" Margin="10,0,0,0"
                    Click="StopRecordingButton_Click"/>

            <Button Grid.Row="2" Grid.ColumnSpan="2" Content="Take Snapshot"
                    Height="24" Width="120" HorizontalAlignment="Center" 
                    Click="TakeSnapshotButton_Click"/>
        </Grid>       
    </Grid>
</Window>
C#
using System.Windows;
using Microsoft.Expression.Encoder.Devices;
using System.Collections.ObjectModel;

namespace WPF_Webcam_CS
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {       
        public Collection<EncoderDevice> VideoDevices { get; set; }
        public Collection<EncoderDevice> AudioDevices { get; set; }

        public MainWindow()
        {
            InitializeComponent();

            this.DataContext = this;

            VideoDevices = EncoderDevices.FindDevices(EncoderDeviceType.Video);
            AudioDevices = EncoderDevices.FindDevices(EncoderDeviceType.Audio);
        }

        private void StartCaptureButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // Display webcam video
                WebcamViewer.StartPreview();
            }
            catch (Microsoft.Expression.Encoder.SystemErrorException ex)
            {
                MessageBox.Show("Device is in use by another application");
            }
        }

        private void StopCaptureButton_Click(object sender, RoutedEventArgs e)
        {
            // Stop the display of webcam video.
            WebcamViewer.StopPreview();
        }

        private void StartRecordingButton_Click(object sender, RoutedEventArgs e)
        {
            // Start recording of webcam video to harddisk.
            WebcamViewer.StartRecording();
        }

        private void StopRecordingButton_Click(object sender, RoutedEventArgs e)
        {
            // Stop recording of webcam video to harddisk.
            WebcamViewer.StopRecording();
        }

        private void TakeSnapshotButton_Click(object sender, RoutedEventArgs e)
        {
            // Take snapshot of webcam video.
            WebcamViewer.TakeSnapshot();
        }
    }
}
VB.NET
Imports Microsoft.Expression.Encoder.Devices
Imports System.Collections.ObjectModel

Public Class MainWindow

    Public Property VideoDevices As Collection(Of EncoderDevice)
    Public Property AudioDevices As Collection(Of EncoderDevice)

    Public Sub New()
        InitializeComponent()

        DataContext = Me

        VideoDevices = EncoderDevices.FindDevices(EncoderDeviceType.Video)
        AudioDevices = EncoderDevices.FindDevices(EncoderDeviceType.Audio)
    End Sub

    Private Sub StartCaptureButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        ' Display webcam video
        Try
            WebcamViewer.StartPreview()
        Catch ex As Microsoft.Expression.Encoder.SystemErrorException
            MessageBox.Show("Device is in use by another application")
        End Try
    End Sub

    Private Sub StopCaptureButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
        ' Stop the display of webcam video
        WebcamViewer.StopPreview()
    End Sub

    Private Sub StartRecordingButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        ' Start recording of webcam video
        WebcamViewer.StartRecording()
    End Sub

    Private Sub StopRecordingButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
        ' Stop recording of webcam video
        WebcamViewer.StopRecording()
    End Sub

    Private Sub TakeSnapshotButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        ' Take snapshot of webcam video
        WebcamViewer.TakeSnapshot()
    End Sub
End Class

In the code behind for MainWindow the collection properties, VideoDevices and AudioDevices, are set with the available audio and video devices. The rest of the code is self-explanatory, the event handlers for the button click events call various control functions.

Webcam

The following is the XAML markup for the user control,

XML
<UserControl x:Class="Webcam" 
             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"
             xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
             MinHeight="100" MinWidth="100"
             mc:Ignorable="d">
    <Grid>
        <WindowsFormsHost Name="WinFormsHost" Margin="0" Background="{x:Null}">
            <wf:Panel x:Name="WebcamPanel"/>
        </WindowsFormsHost>
    </Grid>
</UserControl>

To display video from a webcam the user control makes use of the LiveJob class, which is in the Microsoft.Expression.Encoder.Live namespace. LiveJob expose routines for encoding video and audio from a live source such as a webcam. The webcam video is displayed in a WinForms Panel which is hosted in a WindowsFormsHost.

VB.NET
''' <summary>
''' Displays webcam video.
''' </summary>
Public Sub StartPreview()
    Try
        If isPreviewing Then StopPreview()

        Job = New LiveJob
        Dim frameDuration As Long = CLng(FrameRate * Math.Pow(10, 7))

        deviceSource = Job.AddDeviceSource(_videoDevice, _audioDevice)
        deviceSource.PickBestVideoFormat(FrameSize, frameDuration)
        deviceSource.PreviewWindow = New PreviewWindow(New HandleRef(WebcamPanel, WebcamPanel.Handle))

        Job.OutputFormat.VideoProfile = New AdvancedVC1VideoProfile With {.Size = FrameSize,
                .FrameRate = FrameRate, .Bitrate = New ConstantBitrate(Bitrate)}

        Job.ActivateSource(deviceSource)

        isPreviewing = True
    Catch ex As SystemErrorException
        Throw New SystemErrorException
    End Try
End Sub

The LiveJob object is used to save webcam videos using the LiveJob.StartEncoding() method.

VB.NET
''' <summary>
''' Starts the recording of webcam images to a video file.
''' </summary>
Public Function StartRecording() As String
    If Not isPreviewing Then Throw New PreviewNotStartedException("Recording can't be done without previewing.")
    If String.IsNullOrEmpty(VideoDirectory) Then Throw New DirectoryNotSpecifiedException("Video directory has not been specified.")
    If Not Directory.Exists(VideoDirectory) Then Directory.CreateDirectory(VideoDirectory)
    If IsRecording Then StopRecording()

    Dim filePath As String
    If String.IsNullOrEmpty(VideoName) Then
        filePath = Path.Combine(VideoDirectory, "Webcam " & TimeStamp() & VideoFileFormat)
    Else
        filePath = Path.Combine(VideoDirectory, VideoName & VideoFileFormat)
    End If

    Dim archiveFormat As New FileArchivePublishFormat(filePath)

    If Job.PublishFormats.Count > 0 Then Job.PublishFormats.Clear()

    Job.PublishFormats.Add(archiveFormat)
    Job.StartEncoding()

    SetValue(IsRecordingPropertyKey, True)
    Return filePath
End Function

A snapshot of a webcam video is actually just a snapshot of the WinForms Panel.

VB.NET
''' <summary>
''' Takes a snapshot of an webcam image.
''' The size of the image will be equal to the size of the control.
''' </summary>
Public Function TakeSnapshot() As String
    If Not isPreviewing Then Throw New PreviewNotStartedException("Recording can't be done before previewing.")
    If String.IsNullOrEmpty(ImageDirectory) Then Throw New DirectoryNotSpecifiedException("Image directory has not been specified")
    If Not Directory.Exists(ImageDirectory) Then Directory.CreateDirectory(ImageDirectory)

    Dim panelWidth As Integer = WebcamPanel.Width
    Dim panelHeight As Integer = WebcamPanel.Height
    Dim filePath As String = Path.Combine(ImageDirectory, "Snapshot " & TimeStamp() & "." & SnapshotFormat.ToString())
    Dim pnt As Point = WebcamPanel.PointToScreen(New Point(WebcamPanel.ClientRectangle.X, WebcamPanel.ClientRectangle.Y))

    Using bmp As New Bitmap(panelWidth, panelHeight)
        Using grx As Graphics = Graphics.FromImage(bmp)
            grx.CopyFromScreen(pnt, Point.Empty, New Size(panelWidth, panelHeight))
        End Using
        bmp.Save(filePath, SnapshotFormat)
    End Using

    Return filePath
End Function

Public Function TakeSnapshot(ByVal name As String) As String
    If String.IsNullOrEmpty(name) Then Throw New ArgumentNullException()
    If Not isPreviewing Then Throw New PreviewNotStartedException("Recording can't be done before previewing.")
    If String.IsNullOrEmpty(ImageDirectory) Then Throw New DirectoryNotSpecifiedException("Image directory has not been specified")
    If Not Directory.Exists(ImageDirectory) Then Directory.CreateDirectory(ImageDirectory)

    Dim panelWidth As Integer = WebcamPanel.Width
    Dim panelHeight As Integer = WebcamPanel.Height
    Dim filePath As String = Path.Combine(ImageDirectory, name & "." & SnapshotFormat.ToString())
    Dim pnt As Point = WebcamPanel.PointToScreen(New Point(WebcamPanel.ClientRectangle.X, WebcamPanel.ClientRectangle.Y))

    Using bmp As New Bitmap(panelWidth, panelHeight)
        Using grx As Graphics = Graphics.FromImage(bmp)
            grx.CopyFromScreen(pnt, Point.Empty, New Size(panelWidth, panelHeight))
        End Using
        bmp.Save(filePath, SnapshotFormat)
    End Using

    Return filePath
End Function

If you want to take a look at the rest of the code for the usercontrol check out the user control library project in the source download.

Conclusion

I hope you found this article useful. In case of any queries you can leave a comment and I'll do my best to answer.

History

  • 18th Nov, 2011: Initial post
  • 19th Nov, 2011: Updated code
  • 31st Mar, 2012: Added snapshot feature.
  • 17th Nov, 2012: Added FrameRate and FrameSize properties.
  • 30th Oct, 2013: v3.0
  • 24th July, 2014: v3.1,
    • Webcam preview now resizes with control,
    • StartCapture() renamed to StartPreview(),
    • StopCapture() renamed to StopPreview().
  • 6th May, 2016: Added Bitrate and VideoName properties.
  • 24th Sep, 2017: Updated code – StartRecording() and TakeSnapshot() return full file path.

License

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


Written By
Software Developer
Kenya Kenya
Experienced C# software developer with a passion for WPF.

Awards,
  • CodeProject MVP 2013
  • CodeProject MVP 2012
  • CodeProject MVP 2021

Comments and Discussions

 
QuestionIs it available in x64? Pin
Member 1100708826-Oct-23 22:20
Member 1100708826-Oct-23 22:20 
QuestionInvalid Markup Pin
lukeer1-Oct-20 4:03
lukeer1-Oct-20 4:03 
QuestionCould not load file or assembly 'Microsoft.Expression.Encoder.Utilities Pin
eyanson8-Aug-20 9:43
eyanson8-Aug-20 9:43 
QuestionRe: Could not load file or assembly 'Microsoft.Expression.Encoder.Utilities Pin
Member 1483188312-Aug-20 3:07
Member 1483188312-Aug-20 3:07 
AnswerRe: Could not load file or assembly 'Microsoft.Expression.Encoder.Utilities Pin
chemita_cpu27-Jan-21 2:52
chemita_cpu27-Jan-21 2:52 
QuestionWhite-Balance Function Pin
Member 1347227025-Jun-20 6:46
Member 1347227025-Jun-20 6:46 
QuestionVideo format Pin
Jakub Sklenář22-Jun-20 3:27
Jakub Sklenář22-Jun-20 3:27 
QuestionParameter Not Valid exception after ~10 start/stop captures Pin
Member 148503283-Jun-20 3:25
Member 148503283-Jun-20 3:25 
QuestionError trying to use the Control Pin
Mer60612-Feb-20 8:54
Mer60612-Feb-20 8:54 
QuestionDevice is in use by another application Pin
FlyingOliver6-Feb-20 5:34
FlyingOliver6-Feb-20 5:34 
QuestionApp is working fine now, issue while publishing Pin
mabrali18-Nov-19 10:33
mabrali18-Nov-19 10:33 
QuestionVideo and Image Saving Pin
mabrali18-Nov-19 1:24
mabrali18-Nov-19 1:24 
QuestionWorks great! Is it possible to zoom with this control? Pin
Member 1070323324-Oct-19 4:16
Member 1070323324-Oct-19 4:16 
QuestionWebcam black on Microsoft Surface, not on Lenovo laptop Pin
Member 145769444-Sep-19 21:24
Member 145769444-Sep-19 21:24 
Question'System.InvalidCastException' occurred in Microsoft.Expression.Encoder.dll Pin
Member 145720722-Sep-19 22:31
Member 145720722-Sep-19 22:31 
Questionis the download file you inserted directly contains the project file Pin
MR.SATISH KUMAR NAGOTHI11-Jun-19 23:12
MR.SATISH KUMAR NAGOTHI11-Jun-19 23:12 
QuestionFrame Count Pin
AHKhalid12-Jul-18 10:19
AHKhalid12-Jul-18 10:19 
QuestionTaking a picture directly from the camara Pin
Member 139043879-Jul-18 8:11
Member 139043879-Jul-18 8:11 
AnswerRe: Taking a picture directly from the camara Pin
Meshack Musundi13-Aug-18 9:58
professionalMeshack Musundi13-Aug-18 9:58 
QuestionError Could not load file or assembly 'Microsoft.Expression.Encoder, Version=4.0.0.0' Pin
Ejrr108521-Jun-18 8:45
Ejrr108521-Jun-18 8:45 
QuestionVideo Not Recording Pin
Member 1323872728-May-18 23:59
Member 1323872728-May-18 23:59 
GeneralRe: Video Not Recording Pin
Meshack Musundi13-Aug-18 9:56
professionalMeshack Musundi13-Aug-18 9:56 
QuestionOnly one webcam streamable Pin
Member 138101013-May-18 4:40
Member 138101013-May-18 4:40 
GeneralRe: Only one webcam streamable Pin
Meshack Musundi13-May-18 21:29
professionalMeshack Musundi13-May-18 21:29 
QuestionResolution Pin
dharmabatra6-Apr-18 4:36
dharmabatra6-Apr-18 4:36 

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

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