How to capture image from webcam in C#
By FoxLearn 11/13/2024 6:08:53 AM 4
How to capture image from webcam in c# windows application?
First, Create a new Windows Forms Application,
then add a PictureBox
to display the webcam feed and add a Button
to trigger the image capture.
Next, you need to install the AForge.Video
and AForge.Video.DirectShow
NuGet packages.
You can install them using the NuGet Package Manager:
Here is a sample code to achieve the webcam capture in C#
FilterInfoCollection videoDevices; // To store the available video devices VideoCaptureDevice videoSource; // To capture video from the webcam Bitmap currentFrame; // Current frame from webcam
When the form is loaded, the program searches for available video devices. It selects the first available device and starts capturing frames from it.
private void Form1_Load(object sender, EventArgs e) { // Get available video devices videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); if (videoDevices.Count == 0) { MessageBox.Show("No video devices found.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } // Use the first available video device videoSource = new VideoCaptureDevice(videoDevices[0].MonikerString); // Attach the NewFrame event to capture frames videoSource.NewFrame += VideoSource_NewFrame; // Start capturing the video videoSource.Start(); }
The NewFrame
event is triggered whenever a new frame is available. This frame is captured as a Bitmap
and displayed in a PictureBox
.
private void VideoSource_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs) { // Dispose of the previous frame if (currentFrame != null) currentFrame.Dispose(); // Capture the current frame currentFrame = (Bitmap)eventArgs.Frame.Clone(); // Display the frame in the PictureBox pictureBox.Image = currentFrame; }
When the user clicks the capture button, the current frame is saved to disk as a .jpg
image.
private void btnCapture_Click(object sender, EventArgs e) { if (currentFrame != null) { using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "JPG|*.jpg" }) { if (sfd.ShowDialog() == DialogResult.OK) { // Save the captured image currentFrame.Save(sfd.FileName, System.Drawing.Imaging.ImageFormat.Jpeg); MessageBox.Show("Image captured and saved.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } }
When the form is closed, the video capture device is stopped and the resources are released.
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (videoSource.IsRunning) { videoSource.SignalToStop(); videoSource.WaitForStop(); } if (currentFrame != null) currentFrame.Dispose(); }
Finally, run the application, then press the Capture button to capture an image from the webcam and save it to disk.
If you have multiple webcams, you may need to adjust which one to select by modifying videoDevices[0]
to a different index.
- How to get connection string from app.config in C#
- How to restart program in C#
- How to call SQL Function in C#
- How to convert GUID to Integer in C#
- Curly Braces in string.Format in C#
- How to get process handle from process name in C#
- How to check the size of HTTP resource in C#
- How to Convert long date to short date in C#