How to capture image from webcam in C#

By FoxLearn 11/13/2024 6:08:53 AM   4
To capture an image from a webcam in a C# Windows Forms application, you can use the AForge.NET

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.

capture image from webcam in c#

Next, you need to install the AForge.Video and AForge.Video.DirectShow NuGet packages.

You can install them using the NuGet Package Manager:

aforge.net

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.