Reading Barcodes in .Net
How to Read Barcodes in C# and VB.NET
- Install IronBarcode from Nuget or the DLL download
- Use the BarcodeReader.QuicklyReadOneBarcode method to read any barcode or QR
- Read Multiple Barcodes or QRs in a single scan, PDF, or a multiframe Tiff file
- Allow Iron Barcode to read from imperfect scans and photos
- Download the tutorial project and get scanning now
Install the Barcode Library to your Visual Studio Project
Iron Barcode provides a versatile, advanced, and efficient library for reading barcodes in .NET.
The first step will be to install Iron Barcode, and this is most easily achieved using our NuGet package, although you may also choose to manually install the DLL to your project or to your global assembly cache. IronBarcode works well to produce a C# Barcode Scanner application.
PM > Install-Package Barcode
Read your First Barcode
Reading a Barcode or QR Code in .NET is incredibly easy using the Iron Barcode class library. In our first example, we can see how to read this Barcode with one line of code.
We can extract its value, its image, its encoding type, its binary data (if any), and we can then output that to the console.
- using IronBarCode;
- using System;
- BarcodeResult Result = BarcodeReader.QuicklyReadOneBarcode(“GetStarted.png”);
- if (Result !=null && Result.Text == “https://ironsoftware.com/csharp/barcode/”)
- {
- Console.WriteLine(“GetStarted was a success. Read Value: ” + Result.Text);
- }
Copy code to clipboardVB C#
Try Harder and Be Specific
In this next example, we will add our TryHarder variable to the QuicklyReadOneBarcode method. This makes it try harder, literally taking more time, but scanning deeper for a QR code that might be obscured, corrupted, or at a skewed angle.
- BarcodeResult Result = BarcodeReader.QuicklyReadOneBarcode(“TryHarderQR.png”, BarcodeEncoding.QRCode | BarcodeEncoding.Code128 , true);
Copy code to clipboardVB C#
Which will read this skewed QR Code:
In our example, you can see that we can specify the barcode encoding(s) that we are looking for or specify multiple formats. Doing so greatly improves barcode reading performance and acuracy. The | pipe character or ‘Bitwize OR’ is used to specify multiple formats simultaneously.
The same can be achieved but with a higher degree of specificity if we move forwards to use the BarcodeReader.ReadASingleBarcode Method.
- BarcodeResult Result = IronBarCode.BarcodeReader.ReadASingleBarcode(“TryHarderQR.png”, BarcodeEncoding.QRCode | BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.High, BarcodeReader.BarcodeImageCorrection.MediumCleanPixels);
Copy code to clipboardVB C#
Reading Multiple Barcodes
PDF Documents
In our next example, we are going to look at reading a scanned PDF document and find all of the barcodes of one-dimensional format in very few lines of code.
As you can see, it is very similar to reading a single barcode from a single document, except we now have new information about which page number the barcode was found on.
- using IronBarCode;
- using System;
- using System.Drawing;
- // Multiple barcodes may be scanned up from a single document or image. A PDF document may also used as the input image
- PagedBarcodeResult[] PDFResults = BarcodeReader.ReadBarcodesFromPdf(“MultipleBarcodes.pdf”);
- // Work with the results
- foreach (var PageResult in PDFResults)
- {
- string Value = PageResult.Value;
- int PageNum = PageResult.PageNumber;
- System.Drawing.Bitmap Img = PageResult.BarcodeImage;
- BarcodeEncoding BarcodeType = PageResult.BarcodeType;
- byte[] Binary = PageResult.BinaryValue;
- Console.WriteLine(PageResult.Value+” on page “+ PageNum);
- }
Copy code to clipboardVB C#
We find the following barcodes, on different pages.
Scans and TIFFs
In our next example, we can see that the same result may be found from a multi-frame TIFF, which will be treated similar to a PDF in this respect.
- // Multi frame TIFF and GIF images can also be scanned, and multiple threads will be used automatically in the background for improved performance
- PagedBarcodeResult[] MultiFrameResults = BarcodeReader.ReadBarcodesFromMultiFrameTiff(“Multiframe.tiff”, BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.High, BarcodeReader.BarcodeImageCorrection.MediumCleanPixels);
- foreach (var PageResult in MultiFrameResults)
- {
- //…
- }
Copy code to clipboardVB C#
MultiThreading
To read multiple documents, we get better results from Iron Barcode by creating a list of the documents and using the BarcodeReader.ReadBarcodesMultithreaded method. This uses multiple threads and potentially all cores of your CPU for the barcode scanning process and can be exponentially faster than reading barcodes one at a time.
- // The BarcodeResult.ReadBarcodesMultiThreaded method allows for faster barcode scanning of multiple images or PDFs. All threads are automatically managed by IronBarCode.
- var ListOfDocuments = new[] { “Image1.png”, “image2.JPG”, “image3.pdf” };
- PagedBarcodeResult[] BatchResults = BarcodeReader.ReadBarcodesMultiThreaded(ListOfDocuments);
- // Work with the results
- foreach (var Result in BatchResults)
- {
- string Value = Result.Value;
- //…
- }
Copy code to clipboardVB C#
Reading Barcodes from Imperfect Images
In real world use cases, we may wish to read barcodes which are not perfect screenshots. They may be imperfect images, scans, or photographs and contain digital noise or be skewed. With most conventional open source .net barcode generator and reader libraries, this would be impossible. However, Iron Barcode makes this incredibly straightforward.
In our next example, we will look at the TryHarder method of QuicklyReadOneBarcode. This single parameter causes Iron Barcode to try to de-skew and read barcodes from imperfect digital samples.
Photographs
In the photographs example, we will set specific barcode rotation correction and barcode image correction to correct for the digital noise and for the skew and perspective and rotation that we might reasonably expect from a cellphone camera.
- using IronBarCode;
- using System;
- using System.Drawing;
- // All BarcodeResult.Read methods provide the developer with control to correct image and photograph correction and straightening rotation and perspective from skewed images
- // * RotationCorrection e.g BarcodeReader.BarcodeRotationCorrection.Extreme un-rotates and removes perspective from barcode images.
- // * ImageCorrection e.g BarcodeReader.BarcodeImageCorrection.DeepCleanPixels separates Barcodes from background imagery and digital noise.
- // * BarcodeEncoding e.g. BarcodeEncoding.Code128 Setting a specific Barcode format improves speed and reduces the risk of false positive results
- // Example with a photo image
- var PhotoResult = BarcodeReader.ReadASingleBarcode(“Photo.png”, BarcodeEncoding.Code128, BarcodeReader.BarcodeRotationCorrection.Medium, BarcodeReader.BarcodeImageCorrection.DeepCleanPixels);
- string Value = PhotoResult.Value;
- System.Drawing.Bitmap Img = PhotoResult.BarcodeImage;
- BarcodeEncoding BarcodeType = PhotoResult.BarcodeType;
- byte[] Binary = PhotoResult.BinaryValue;
- Console.WriteLine(PhotoResult.Value);
Copy code to clipboardVB C#
Scans
The next example shows us how we might approach reading QR codes and PDF-417 barcodes from a scanned PDF. Note that we have set an appropriate level of barcode rotation correction and barcode image correction to lightly clean the document, but not to incur a huge performance penalty by over-egging our needs.
- // Multi frame TIFF and GIF images can also be scanned, and multiple threads will be used automatically in the background for improved performance
- var ScanResults = BarcodeReader.ReadBarcodesFromPdf(“Scan.pdf”, BarcodeEncoding.All, BarcodeReader.BarcodeRotationCorrection.Low, BarcodeReader.BarcodeImageCorrection.LighlyCleanPixels);
- // Work with the results
- foreach (var PageResult in ScanResults)
- {
- string Value = PageResult.Value;
- ///…
- }
Copy code to clipboardVB C#
Thumbnails
In the final example, we will see that Iron Barcode is even capable of reading a corrupted thumbnail of a barcode.
Our barcode reader methods automatically detect barcode images which are too small to reasonably be an actual barcode, and upscale and clean all of the digital noise associated with thumbnailing; allowing them to be readable again.
- // Small or ‘Thumbnail’ barcode images are automatically detected by IronBarCode and corrected for wherever possible even if they have much digital noise.
- BarcodeResult SmallResult = BarcodeReader.QuicklyReadOneBarcode(“ThumbnailOfBarcode.gif”, BarcodeEncoding.Code128);
Copy code to clipboardVB C#
Summary
In summary, Iron Barcode is a versatile .Net software library library for reading a wide range of barcode formats, and it can do so whether or not these barcodes are perfect screen grabs or are in fact photographs, scans, or other imperfect real world images.
Further Reading
To learn more about working with Iron Barcode, you may wish to look at the other tutorials within this section, and also the examples on our homepage; which most developers find enough to get them started.
Our Object Reference with specific reference to the BarcodeReader class and the BarcodeEncoding Enum will show you in detail what you are capable of achieving using this C# Barcode Library.
Source Code Downloads
We also highly encourage you to download this tutorial and run it for yourself. You can do this by downloading the source code, or by forking us on GitHub. The source for this “Barcode Reader” tutorial are available as Visual Studio 2017 Console Application project written in C#.
Frank Walker
.Net Product Developer
Frank has been a founding force in the development of Iron Barcode. Working with IronOCR for the last year, Frank has been influential to building use cases within OCR for Iron Barcode to build out into the tool it is now.