Click here to Skip to main content
15,867,686 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

 
GeneralRe: WebcamControl.dll Pin
Meshack Musundi17-Apr-16 22:17
professionalMeshack Musundi17-Apr-16 22:17 
GeneralRe: WebcamControl.dll Pin
Member 1246467718-Apr-16 3:27
Member 1246467718-Apr-16 3:27 
GeneralRe: WebcamControl.dll Pin
Meshack Musundi18-Apr-16 8:42
professionalMeshack Musundi18-Apr-16 8:42 
Questionwinforms version Pin
Jeric Canatuan18-Feb-16 4:56
Jeric Canatuan18-Feb-16 4:56 
QuestionOrientation not changing in Surface. Pin
linto_1120-Dec-15 0:01
linto_1120-Dec-15 0:01 
QuestionRe: Orientation not changing in Surface. Pin
Meshack Musundi20-Dec-15 21:00
professionalMeshack Musundi20-Dec-15 21:00 
PraiseThank's Pin
Sprenol8-Dec-15 0:00
Sprenol8-Dec-15 0:00 
GeneralRe: Thank's Pin
Meshack Musundi8-Dec-15 6:50
professionalMeshack Musundi8-Dec-15 6:50 
QuestionWindows 8.1 Unhandled Event 0x80040217 Pin
DMoody00720-Oct-15 10:26
DMoody00720-Oct-15 10:26 
AnswerRe: Windows 8.1 Unhandled Event 0x80040217 Pin
Meshack Musundi21-Oct-15 6:33
professionalMeshack Musundi21-Oct-15 6:33 
GeneralRe: Windows 8.1 Unhandled Event 0x80040217 Pin
DMoody00721-Oct-15 7:05
DMoody00721-Oct-15 7:05 
QuestionHow to flip! Pin
Member 377712218-Sep-15 7:53
Member 377712218-Sep-15 7:53 
AnswerRe: How to flip! Pin
Meshack Musundi19-Sep-15 1:08
professionalMeshack Musundi19-Sep-15 1:08 
QuestionMicrosoft.Expression.Encoder Pin
Roma Priymak18-Aug-15 5:42
Roma Priymak18-Aug-15 5:42 
AnswerRe: Microsoft.Expression.Encoder Pin
Edex12321-Mar-16 9:08
Edex12321-Mar-16 9:08 
QuestionSnapshot resolution Pin
alessiom2-Jul-15 3:41
alessiom2-Jul-15 3:41 
AnswerRe: Snapshot resolution Pin
nitewulf5028-Jan-16 4:05
nitewulf5028-Jan-16 4:05 
QuestionCan we add Zoom In & out functionality for the camera. Pin
bharat290522-Jun-15 21:33
bharat290522-Jun-15 21:33 
QuestionWhy Windows Forms Panel Pin
Joel Palmer16-Apr-15 19:44
Joel 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
amr ismail30-Mar-15 4:23 
QuestionVideo Director Pin
bruno.picardi27-Mar-15 21:35
bruno.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
bruno.picardi28-Mar-15 1:37 
QuestionSave the Video Pin
Member 879873427-Mar-15 19:05
Member 879873427-Mar-15 19:05 

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.