How to Read Multiple Barcodes at Once in C#
IronBarcode enables simultaneous reading of multiple barcodes from images and PDFs by setting ExpectMultipleBarcodes = true, streamlining data processing for logistics, retail, and inventory management applications. Whether building warehouse systems, retail point-of-sale applications, or document processing solutions, IronBarcode's advanced reading capabilities provide the reliability and performance you need.
This example shows how quickly you can use IronBarcode to scan an image for every barcode it contains. Just set ExpectMultipleBarcodes = true alongside the barcode types you want - no boilerplate, no hassle.
-
1Install IronBarcode with NuGet Package Manager
-
2Copy and run this code snippet.
var results = IronBarCode.BarcodeReader.Read("image.png", new IronBarCode.BarcodeReaderOptions { ExpectMultipleBarcodes = true, ExpectBarcodeTypes = IronBarCode.BarcodeEncoding.AllOneDimensional });C# -
3Deploy to test on your live environment
Start using IronBarcode in your project today with a free trial
Minimal Workflow (5 steps)
- Download the C# library to read multiple barcodes
- Use the
Readmethod to extract barcode values from various image formats - Utilize the
ExpectMultipleBarcodesproperty to configure reading of single or multiple barcodes - Set the
ExpectMultipleBarcodesproperty to false to increase performance - Print out the barcode values
How Do I Read Multiple Barcodes from an Image?
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. To address this, customize the settings to enable reading multiple barcodes, as shown below. The ExpectMultipleBarcodes property exists in both BarcodeReaderOptions and PdfBarcodeReaderOptions classes, allowing you to use it for reading barcodes in both images and PDF documents.

using IronBarCode;
using System;
// Set the option to read multiple barcodes
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
ExpectMultipleBarcodes = true,
ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional,
};
// Read barcode
var results = BarcodeReader.Read("testbc1.png", options);
foreach (var result in results)
{
Console.WriteLine(result.ToString());
}Imports IronBarCode
Imports System
' Set the option to read multiple barcodes
Private options As New BarcodeReaderOptions() With {
.ExpectMultipleBarcodes = True,
.ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional
}
' Read barcode
Private results = BarcodeReader.Read("testbc1.png", options)
For Each result In results
Console.WriteLine(result.ToString())
Next resultSetting ExpectMultipleBarcodes to true enables IronBarcode to scan the entire document for multiple barcodes and store them in the BarcodeResults variable. Using a foreach loop, you can easily access and print all barcode values to the console.
Advanced Multiple Barcode Reading Scenarios
When working with multiple barcodes, you might encounter scenarios that require additional configuration. Here's a comprehensive example demonstrating how to read multiple barcodes with different formats from a complex document:
using IronBarCode;
using System;
using System.Linq;
// Configure advanced options for mixed barcode types
BarcodeReaderOptions advancedOptions = new BarcodeReaderOptions()
{
ExpectMultipleBarcodes = true,
// Read both 1D and 2D barcodes
ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional | BarcodeEncoding.QRCode | BarcodeEncoding.DataMatrix,
// Apply image correction filters for better accuracy
ImageFilters = new ImageFilterCollection() {
new SharpenFilter(),
new ContrastFilter()
},
// Set speed vs accuracy balance
Speed = ReadingSpeed.Balanced
};
// Read barcodes from the image
var imageResults = BarcodeReader.Read("mixed-barcodes.jpg", advancedOptions);
// Process results with error handling
foreach (var result in imageResults)
{
Console.WriteLine($"Barcode Type: {result.BarcodeType}");
Console.WriteLine($"Value: {result.Value}");
Console.WriteLine($"Page: {result.PageNumber}");
Console.WriteLine("---");
}
This advanced example showcases several important features:
- Mixed barcode format support: Combining different barcode encoding types
- Image correction filters: Using image filters to improve reading accuracy
- Reading speed optimization: Balancing speed and accuracy with reading speed options
- Confidence threshold: Filtering results using the confidence threshold reader option
How Can I Read a Single Barcode for Better Performance?
IronBarcode reads both single and multiple barcodes in images or PDFs. By default, the engine scans the entire document even if only one barcode exists. For increased performance when reading a single barcode, set ExpectMultipleBarcodes to false. This stops the engine from scanning the entire document after detecting the first barcode, resulting in faster barcode retrieval. The code below demonstrates this approach.

using IronBarCode;
using System;
// Set the option to read single barcode
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
ExpectMultipleBarcodes = false,
ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional,
};
// Read barcode
var results = BarcodeReader.Read("testbc1.png", options);
foreach (var result in results)
{
Console.WriteLine(result.ToString());
}Imports IronBarCode
Imports System
' Set the option to read single barcode
Private options As New BarcodeReaderOptions() With {
.ExpectMultipleBarcodes = False,
.ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional
}
' Read barcode
Private results = BarcodeReader.Read("testbc1.png", options)
For Each result In results
Console.WriteLine(result.ToString())
Next resultIn this example, we used the same image with multiple barcodes as before but 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.
Optimizing Single Barcode Reading with Crop Regions
For even better performance when reading single barcodes, combine the ExpectMultipleBarcodes = false setting with crop region specifications. This technique is particularly useful when you know the approximate location of your barcode:
using IronBarCode;
using IronSoftware.Drawing;
// Define a crop region where the barcode is likely located
var cropRegion = new Rectangle(100, 100, 300, 200);
// Configure options for optimal single barcode reading
BarcodeReaderOptions optimizedOptions = new BarcodeReaderOptions()
{
ExpectMultipleBarcodes = false,
ExpectBarcodeTypes = BarcodeEncoding.Code128,
CropArea = cropRegion,
Speed = ReadingSpeed.Faster
};
// Read with optimized settings
var result = BarcodeReader.Read("product-label.png", optimizedOptions).FirstOrDefault();
if (result != null)
{
Console.WriteLine($"Barcode found: {result.Value}");
}
How Much Faster Is Single Barcode Reading?
Setting ExpectMultipleBarcodes = false greatly improves the efficiency of reading single barcodes. The performance gain is particularly noticeable when working with high-resolution images or when implementing asynchronous barcode reading in high-throughput applications.
Setting ExpectMultipleBarcodes = false lets the engine stop after detecting the first barcode instead of scanning the entire document, which can noticeably reduce read time for single-barcode workloads. The actual performance gain varies based on:
- Image resolution and complexity
- Number of barcodes present in the image
- Selected barcode formats
- Applied image filters
- Hardware specifications
Best Practices for Multiple Barcode Reading
When implementing multiple barcode reading in production applications, consider these best practices:
- Specify Expected Barcode Types: Instead of using
BarcodeEncoding.All, specify only the formats you expect. This significantly improves performance. - Use Appropriate Image Formats: For best results, use high-contrast images. Learn more about creating optimal barcode images.
- Handle Imperfect Barcodes: Real-world barcodes may be damaged or poorly printed. Use image correction techniques to improve reading success rates.
- Stream Processing: For large batches, consider reading from streams to optimize memory usage.
- Error Handling: Always implement proper error handling for scenarios where barcodes cannot be read:
try
{
var results = BarcodeReader.Read("barcodes.png", new BarcodeReaderOptions
{
ExpectMultipleBarcodes = true
});
if (!results.Any())
{
Console.WriteLine("No barcodes found in the image");
}
else
{
Console.WriteLine($"Found {results.Count()} barcodes");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error reading barcodes: {ex.Message}");
// Log error for debugging
}Try
Dim results = BarcodeReader.Read("barcodes.png", New BarcodeReaderOptions With {
.ExpectMultipleBarcodes = True
})
If Not results.Any() Then
Console.WriteLine("No barcodes found in the image")
Else
Console.WriteLine($"Found {results.Count()} barcodes")
End If
Catch ex As Exception
Console.WriteLine($"Error reading barcodes: {ex.Message}")
' Log error for debugging
End TryBy following these practices and utilizing IronBarcode's comprehensive features, you can build robust applications that efficiently handle multiple barcode reading scenarios across various industries and use cases.
Frequently Asked Questions
How do I enable multiple barcode scanning in IronBarcode?
To enable multiple barcode scanning in IronBarcode, set the 'ExpectMultipleBarcodes' property to true within the BarcodeReaderOptions or PdfBarcodeReaderOptions classes.
What is the purpose of the ExpectMultipleBarcodes property in IronBarcode?
The 'ExpectMultipleBarcodes' property allows IronBarcode to read multiple barcodes from an image or PDF simultaneously, optimizing data extraction in logistics and retail applications.
Can IronBarcode read both 1D and 2D barcodes?
Yes, IronBarcode can read both 1D and 2D barcodes. You can specify the types of barcodes to expect by configuring the 'ExpectBarcodeTypes' property in BarcodeReaderOptions.
How do I increase the performance for reading only one barcode using IronBarcode?
To increase performance when reading a single barcode, set 'ExpectMultipleBarcodes' to false in the BarcodeReaderOptions. This stops the scanning process after detecting the first barcode.
What configurations are available for reading multiple barcodes with different formats?
IronBarcode offers advanced options like combining different barcode decoding types, applying image correction filters, and adjusting reading speed for handling multiple barcode formats.
How can image correction enhance barcode reading in IronBarcode?
Applying image correction filters, such as the SharpenFilter and ContrastFilter, can enhance barcode reading accuracy by compensating for poor image quality or damaged barcodes.
What strategies are recommended for optimal barcode reading performance?
For optimal performance, specify expected barcode types, use high-contrast images, implement image correction techniques, read from streams for large batches, and include error handling.
How can I handle errors while reading barcodes with IronBarcode?
Implement proper error handling using try-catch blocks to manage scenarios where barcodes cannot be read, and log errors for debugging if necessary.
Is stream processing supported in IronBarcode for large barcode batches?
Yes, IronBarcode supports reading barcodes from streams, which is particularly useful for optimizing memory usage in high-throughput applications or large batch processing.
