Windows Forms: Barcode Scanner using Webcam in C#

This post shows you How to Create Barcode Scanner using Webcam in C# Windows Forms Application.

This is a simple example of how to create a barcode scanner using a webcam in C#. For this example, we'll be using the ZXing.Net library to create a barcode image or decode a barcode image, which is a popular open-source library for reading barcodes.

Next, We will use AForge.NET library to capture frames from the webcam.

How to read barcode using webcam in C#?

Creating a new Windows Forms Application, then open your form designer.

To create a barcode scanner in c# windows application, you can drag and drop Label, Combobox, PictureBox, TextBox and Button controls from the Visual Studio toolbox to your form designer.

Next, You can design a simple user interface that allows you to a detect webcam or camera, then add the device name to the Combobox control.

Finally, We will decode the barcode image taken from the webcam, then update the image taken from the webcam to the PictureBox control.

c# barcode scanner using webcam

After you complete your form design, you need to right-click your project, then select Manage Nuget Packages.

Next, Search and Install 'AForge.Video.DirectShow', 'ZXing.NET' to your project.

You can install the ZXing.Net library via NuGet Package Manager Console by running the following command.

Install-Package ZXing.Net

Similar, to install the AForge.Video and AForge.Video.DirectShow packages via NuGet Package Manager Console

Install-Package AForge.Video
Install-Package AForge.Video.DirectShow

Barcode scanner library c#

ZXing.NET is a c# barcode reader open source or qr code reader c# open source. 

aforge zxing barcode reader c# example

Declare two variables as shown below.

FilterInfoCollection filterInfoCollection;
VideoCaptureDevice videoCaptureDevice;

And don't forget to include the namespaces below.

using AForge.Video.DirectShow;
using ZXing;

Open your form designer, then add a Form_Load event handler that allows you to detect your webcam or camera name.

// Set up the video capture device (webcam)
private void Form1_Load(object sender, EventArgs e)
    filterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
    foreach (FilterInfo device in filterInfoCollection)
    cboCamera.SelectedIndex = 0;

Adding a click event handler to the Start button that allows you to capture picture from the webcam.

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

And don't forget to add a NewFrame event handler allows you to decode barcode image from webcam.

private void VideoCaptureDevice_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
    Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone();
    // Create a barcode reader instance
    BarcodeReader reader = new BarcodeReader();
    // Decode the barcode
    var result = reader.Decode(bitmap);
    if (result != null)
        // If a barcode is detected, print its content
        txtBarcode.Invoke(new MethodInvoker(delegate ()
            txtBarcode.Text = result.ToString();
    pictureBox.Image = bitmap;

Finally, Add a FormClosing event handler allows you to stop your webcam when closing your form.

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    if (videoCaptureDevice != null)
        if (videoCaptureDevice.IsRunning)

This is free barcode scanner using webcam in c#. So you're done creating an app that lets you read barcodes from your webcam in C#. You can do the same way to help you scan qr code using webcam in c#.

You can easily create a free barcode online by acessing this website: Link

Or you can view the post "How to create a barcode image in C#" to know how to create a barcode image in C#.