Get started with IronBarcode

Start using IronBarcode in your project today with a free trial.

First Step:
green arrow pointer



Read Multiple Barcodes Example

By default, IronBarcode continuously scans a document to read multiple barcodes. However, there have been instances where only one barcode value is returned, even when multiple barcodes are present in the image. To address this, users can customize the settings to enable reading multiple barcodes, as shown in the code snippet below. Please note that the ExpectMultipleBarcodes property exists in both the BarcodeReaderOptions and PdfBarcodeReaderOptions classes, allowing users to use it for reading barcodes in both images and PDF documents.

Sample Image

Image to be read
:path=/static-assets/barcode/content-code-examples/how-to/read-multiple-barcodes-read-multiple-barcodes.cs
using IronBarCode;
using System;

// Initialize Barcode reader options to read multiple barcodes
BarcodeReaderOptions options = new BarcodeReaderOptions
{
    // Configure to expect multiple barcodes in the image being processed
    ExpectMultipleBarcodes = true,

    // Specify that we expect any one-dimensional barcode types (like Code128, UPC, etc.)
    ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional,
};

// Read barcodes from the specified image file "testbc1.png" using the configured options
BarcodeResult[] results = BarcodeReader.Read("testbc1.png", options);

// Iterate over each decoded barcode result
foreach (BarcodeResult result in results)
{
    // Output the barcode content to the console
    Console.WriteLine(result.ToString());
}
Imports IronBarCode

Imports System



' Initialize Barcode reader options to read multiple barcodes

Private options As New BarcodeReaderOptions With {

	.ExpectMultipleBarcodes = True,

	.ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional

}



' Read barcodes from the specified image file "testbc1.png" using the configured options

Private results() As BarcodeResult = BarcodeReader.Read("testbc1.png", options)



' Iterate over each decoded barcode result

For Each result As BarcodeResult In results

	' Output the barcode content to the console

	Console.WriteLine(result.ToString())

Next result
$vbLabelText   $csharpLabel

By setting ExpectMultipleBarcodes to true in the code snippet, IronBarcode scans the entire document for multiple barcodes and stores them in the BarcodeResults variable. Using a foreach loop, users can easily access and print all the barcode values to the console.

Reading Single Barcode Example

IronBarcode can read both single and multiple barcodes in an image or PDF. By default, the engine scans the entire document even if there is only one barcode. However, for increased performance when reading a single barcode, you can set ExpectMultipleBarcodes to false. This stops the engine from scanning the entire document after the first barcode is detected, resulting in faster barcode retrieval. The code snippet below demonstrates how to do this.

Sample Image

Image to be read
:path=/static-assets/barcode/content-code-examples/how-to/read-multiple-barcodes-read-single-barcode.cs
using IronBarCode;
using System;

// Entry point of the program
class Program
{
    static void Main()
    {
        // Create options to configure the barcode reader
        BarcodeReaderOptions options = new BarcodeReaderOptions
        {
            // Expect only one barcode per image
            ExpectMultipleBarcodes = false,
            
            // Specify the expected types of barcodes (one-dimensional barcodes in this case)
            ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional
        };

        // Read barcode from a specified image file with the given options
        var results = BarcodeReader.Read("testbc1.png", options);

        // Check if results are not null to avoid possible null reference exceptions
        if (results != null)
        {
            // Iterate over all read results and print each barcode as a string
            foreach (var result in results)
            {
                // Print out the barcode value and type for better debugging or display
                Console.WriteLine($"Barcode Value: {result.Text}, Barcode Type: {result.Encoding}");
            }
        }
        else
        {
            // Inform the user that no barcodes were found
            Console.WriteLine("No barcodes were found in the image.");
        }
    }
}
Imports IronBarCode

Imports System



' Entry point of the program

Friend Class Program

	Shared Sub Main()

		' Create options to configure the barcode reader

		Dim options As New BarcodeReaderOptions With {

			.ExpectMultipleBarcodes = False,

			.ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional

		}



		' Read barcode from a specified image file with the given options

		Dim results = BarcodeReader.Read("testbc1.png", options)



		' Check if results are not null to avoid possible null reference exceptions

		If results IsNot Nothing Then

			' Iterate over all read results and print each barcode as a string

			For Each result In results

				' Print out the barcode value and type for better debugging or display

				Console.WriteLine($"Barcode Value: {result.Text}, Barcode Type: {result.Encoding}")

			Next result

		Else

			' Inform the user that no barcodes were found

			Console.WriteLine("No barcodes were found in the image.")

		End If

	End Sub

End Class
$vbLabelText   $csharpLabel

In the code snippet above, we used the same image with multiple barcodes as before, but this time, we set ExpectMultipleBarcodes to false. As a result, only the first barcode value is returned, and the scanning process stops once the first barcode is retrieved.

Performance Comparison

Setting ExpectMultipleBarcodes to false can greatly improve the efficiency of reading single barcodes in the image.

Using the provided code snippet, here's a rough estimate of the performance difference between setting ExpectMultipleBarcodes to true and false on the same machine:

ExpectMultipleBarcodes = true ExpectMultipleBarcodes = false
00.91 second 00.10 second

Frequently Asked Questions

What is IronBarcode?

IronBarcode is a library for reading and writing barcodes in .NET applications, enabling efficient data processing in various industries.

How can I read multiple barcodes using IronBarcode?

To read multiple barcodes, set the ExpectMultipleBarcodes property to true in the BarcodeReaderOptions or PdfBarcodeReaderOptions classes and use the Read method.

Why is reading multiple barcodes simultaneously important?

Reading multiple barcodes simultaneously is crucial for efficient data processing in industries such as logistics, retail, and healthcare.

How do I optimize IronBarcode for reading a single barcode?

Set the ExpectMultipleBarcodes property to false to stop scanning after detecting the first barcode, improving performance.

Can IronBarcode read barcodes from PDF documents?

Yes, IronBarcode can read barcodes from both images and PDF documents by configuring the appropriate reader options.

What are the performance benefits of setting ExpectMultipleBarcodes to false?

Setting ExpectMultipleBarcodes to false greatly improves performance by reducing the time needed to retrieve a single barcode.

What are the steps to read multiple barcodes using IronBarcode?

Download the C# library, configure the ExpectMultipleBarcodes property, use the Read method, and print the barcode values.

What should I do if only one barcode is returned in an image with multiple barcodes?

Ensure that the ExpectMultipleBarcodes property is set to true to enable reading all barcodes present in the image.

Hairil Hasyimi Bin Omar
Hairil Hasyimi Bin Omar
Software Engineer
Like all great engineers, Hairil is an avid learner. He’s refining his knowledge of C#, Python, and Java, using that knowledge to add value to team members across Iron Software. Hairil joined the Iron Software team from Universiti Teknologi MARA in Malaysia, where he graduated with a Bachelor's degree in Chemical and Process Engineering.