Windows Forms: How to use webcam in C#

This post shows you how to access webcam using AForge.NET in C# .NET Windows Forms Application.

To play the demo, you should either prepare a webcam or you can use the webcam from your Laptop.

How to integrate webcam in C#?

It's better to create a new Windows Forms Application project, then open your form designer.

Next, Drag the Label, Combobox, PictureBox and Button controls from the Visual Studio toolbox to your winform, then you can layout your UI as shown below.

c# webcam example

C# Web camera using Aforge.net

Next, Right click on your project, then select Nuget Manage Packages => Search 'AForge' => install AForge.Video and AForge.Video.DirectShow to your project.

Using the AForge.Video and AForge.Video.DirectShow libraries, you can easily integrate webcam in c#.

The AForge.Video contains interfaces and classes that help you access different video sources, such as IP video cameras. This library defines types used by other video related libraries from AForge.NET framework.

C# DirectShow video capture example

The AForge.Video.DirectShow contains classes that allow you to access video sources using the DirectShow interface. Such as, USB web cameras, capture devices, video files, etc.

How to use webcam in c# windows application?

To use webcam in c#, you need to declare two variables as the following c# video capture sample.

FilterInfoCollection filterInfoCollection;
VideoCaptureDevice videoCaptureDevice;

Next, Add a Form_Load event handler to delect your camera device, then add the device to the combobox control.

private void Form1_Load(object sender, EventArgs e)
{
    filterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
    foreach (FilterInfo Device in filterInfoCollection)
        cboCamera.Items.Add(Device.Name);
    cboCamera.SelectedIndex = 0;
    videoCaptureDevice = new VideoCaptureDevice();
}

C# Camera Capture Example

Adding a click event handler to the Start button allows you to read webcam image in c#.

private void btnStart_Click(object sender, EventArgs e)
{
    videoCaptureDevice = new VideoCaptureDevice(filterInfoCollection[cboCamera.SelectedIndex].MonikerString);
    videoCaptureDevice.NewFrame += FinalFrame_NewFrame;
    videoCaptureDevice.Start();
}

Webcam integration in c# windows application

You need to add the FinalFrame_NewFrame event handler to capture image from the camera, then display into the PictureBox control.

//c# webcam capture picture
private void FinalFrame_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
    picCamera.Image = (Bitmap)eventArgs.Frame.Clone();
}

And don't forget to stop the camera when the form closing.

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (videoCaptureDevice.IsRunning == true)
        videoCaptureDevice.Stop();
}

To access webcam in c# you need to import the namespaces below.

using AForge.Video;
using AForge.Video.DirectShow;

Press F5 to rebuild and run your application, click the Start button you can see your webcam open and capture image from webcam to the PictureBox control in c# windows application.

VIDEO TUTORIAL