How to use webcam in C#
By FoxLearn 11/27/2024 11:38:44 AM 31.72K
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 and drop the Label, Combobox, PictureBox and Button controls from the Visual Studio toolbox to your winform, then design your Windows Form application with necessary UI elements like buttons for starting the webcam, and a picture box to display the video feed.
C# Web camera using Aforge.net
You need to install the AForge.NET Framework. You can do this via NuGet Package Manager in Visual Studio by right-clicking 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.
// video capture c# 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.
In this example cboCamera
is a ComboBox where available webcam devices are listed, btnStart
is the button to start the webcam, picCamera
is the PictureBox control where the video feed will be displayed.
VIDEO TUTORIAL