How to Read Barcodes in C# for .NET 5 using Iron Barcode

In this tutorial, you will learn how to read barcodes in C# using the Iron Barcode library. First, open Visual Studio and navigate to the Package Manager Console to install the Iron Barcode library by typing:

Install-Package BarCode

Once installed, proceed to the Program.cs file to write the necessary code. Begin by adding the proper namespace:

using IronBarCode;
using IronBarCode;
Imports IronBarCode
$vbLabelText   $csharpLabel

Reading a barcode is simplified with the method BarcodeReader.QuicklyReadOneBarcode, which takes the file name of the barcode image as a parameter. Here is an example showing how to use this method:

using System;
using IronBarCode;

namespace BarcodeReaderExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // File path to the barcode image
            string barcodeImagePath = "barcode.png";

            // Try to read a single barcode from the image
            var barcodeResult = BarcodeReader.QuicklyReadOneBarcode(barcodeImagePath);

            // Check if the barcode was successfully read
            if (barcodeResult != null)
            {
                // Print the decoded barcode value to the console
                Console.WriteLine($"Barcode Value: {barcodeResult.Text}");
            }
            else
            {
                // Inform the user that no barcode was found
                Console.WriteLine("No barcode found in the image.");
            }
        }
    }
}
using System;
using IronBarCode;

namespace BarcodeReaderExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // File path to the barcode image
            string barcodeImagePath = "barcode.png";

            // Try to read a single barcode from the image
            var barcodeResult = BarcodeReader.QuicklyReadOneBarcode(barcodeImagePath);

            // Check if the barcode was successfully read
            if (barcodeResult != null)
            {
                // Print the decoded barcode value to the console
                Console.WriteLine($"Barcode Value: {barcodeResult.Text}");
            }
            else
            {
                // Inform the user that no barcode was found
                Console.WriteLine("No barcode found in the image.");
            }
        }
    }
}
Imports System
Imports IronBarCode

Namespace BarcodeReaderExample
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			' File path to the barcode image
			Dim barcodeImagePath As String = "barcode.png"

			' Try to read a single barcode from the image
			Dim barcodeResult = BarcodeReader.QuicklyReadOneBarcode(barcodeImagePath)

			' Check if the barcode was successfully read
			If barcodeResult IsNot Nothing Then
				' Print the decoded barcode value to the console
				Console.WriteLine($"Barcode Value: {barcodeResult.Text}")
			Else
				' Inform the user that no barcode was found
				Console.WriteLine("No barcode found in the image.")
			End If
		End Sub
	End Class
End Namespace
$vbLabelText   $csharpLabel

Key Points:

  • Ensure that the barcode.png image is placed in the output directory of your project, typically the bin folder, to make sure it can be accessed when the program runs.
  • The QuicklyReadOneBarcode method attempts to read a single barcode from the provided image file. It returns a BarcodeResult object upon success, or null if no barcode is found.

Running the program will successfully read and display the barcode value. This straightforward approach enables efficient barcode reading using C#.

Further Reading: Read Barcodes in C#

Jordi Bardia
Software Engineer
Jordi is most proficient in Python, C# and C++, when he isn’t leveraging his skills at Iron Software; he’s game programming. Sharing responsibilities for product testing, product development and research, Jordi adds immense value to continual product improvement. The varied experience keeps him challenged and engaged, and he says it’s one of his favorite aspects of working with Iron Software. Jordi grew up in Miami, Florida and studied Computer Science and Statistics at University of Florida.
NEXT >
How to Generate Barcode Images in C# for .NET using Iron Barcode