Windows Forms: Barcode Scanner using Webcam in C#

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

We will use AForge library to capture image from the webcam, then use ZXing.NET library to create a barcode image or decode a barcode image.

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

Dragging Label, Combobox, PictureBox, TextBox and Button controls from the Visual Studio Toolbox to your form designer. 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.

Barcode scanner library c#

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

aforge zxing.net c#

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.

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;
}

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;
    videoCaptureDevice.Start();
}

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();
    BarcodeReader reader = new BarcodeReader();
    var result = reader.Decode(bitmap);
    if (result != null)
    {
        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)
            videoCaptureDevice.Stop();
    }
}

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# .net.

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#.

VIDEO TUTORIAL