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.
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.
Declare two variables as shown below.
And don't forget to include the namespaces below.
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.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();
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)
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#.