How to Read Barcodes from Images Using C#
IronBarcode reads barcodes from images in C# with a single line of code using BarcodeReader.Read(), supporting multiple image formats including PNG, JPEG, GIF, BMP, TIFF, and SVG, with customizable options for improved performance and accuracy.
One of the key features of IronBarcode is its ability to read barcodes out-of-the-box in multiple image formats. The following image formats are currently supported by IronBarcode:
- Scalable Vector Graphics (SVG)
- Joint Photographic Experts Group (JPEG)
- Portable Network Graphics (PNG)
- Graphics Interchange Format (GIF)
- Tagged Image File Format (TIFF)
- Bitmap Image File (BMP)
This is made possible with the help of our open source library, IronDrawing. For a complete list of supported barcode formats, including both 1D and 2D types, check our comprehensive documentation.
Quickstart: Read Barcodes from an Image in SecondsWith just one simple call to IronBarCode.BarcodeReader.Read(), you can extract barcode data straight from image file formats like PNG, JPEG, GIF, BMP, and TIFF. Get started instantly - no complex setup, just instant results.
-
1Install IronBarcode with NuGet Package Manager
-
2Copy and run this code snippet.
var results = IronBarCode.BarcodeReader.Read("path/to/image.png");C# -
3Deploy to test on your live environment
Start using IronBarcode in your project today with a free trial
Minimal Workflow (5 steps)

- Download C# library to read barcode from image
- Use the
Readmethod to read barcode values from various image formats - Utilize the BarcodeReaderOptions class to configure reading settings
- Specify barcode regions in the image with the CropArea property
- Specify barcode types to read by setting the ExpectBarcodeTypes property
How Do I Read Barcodes Directly From Images?
Here's how to use IronBarcode for barcode reading. For a comprehensive tutorial on reading barcodes in C# / .NET, including advanced techniques for PDF processing and batch operations, visit our detailed guide:
using IronBarCode;
using System;
var myBarcode = BarcodeReader.Read(@"image_file_path.jpg"); //image file path
foreach (var item in myBarcode)
{
Console.WriteLine(item.ToString());
}Imports IronBarCode
Imports System
Private myBarcode = BarcodeReader.Read("image_file_path.jpg") 'image file path
For Each item In myBarcode
Console.WriteLine(item.ToString())
Next item

To use IronBarcode, the first thing you have to do is to install the IronBarcode library via Microsoft Visual Studio NuGet package manager into your project, as shown in the picture below. This will allow you to access IronBarcode's BarcodeReader.Read() method to directly read barcode images.
IronBarcode offers simplicity by allowing users to only use BarcodeReader.Read() to read an image file that has already been included inside the project by specifying the file name string, OR file path string as the parameter for the method. Best practice is to use the verbatim string literal, "@" when specifying a file path in the method to avoid adding multiple escape characters "\" in the file path string.
Attach the Values() method at the end of the BarcodeReader.Read() method call to get the barcode value as a System.String[] object.
To output the result to the console, you can use a foreach loop to iterate over the values stored in the string[] array and inside the loop block, call the Console.WriteLine() method with the iterator variable as the parameter.
IronBarcode is capable of reading 1-Dimensional barcode formats (Codabar, Code128, Code39, Code93, EAN13, EAN8, ITF, MSI, UPCA, UPCE) as well as 2-Dimensional barcode formats (Aztec, DataMatrix, QRCode) in various image formats.
How Can I Configure Barcode Reader Options for Better Performance?
Finding barcode reading too slow? Is the barcode too small in the picture, making IronBarcode unable to read it? Want to read only certain areas of an image? Want to read only certain types of barcodes in an image with a mixture of barcodes? Want to improve overall reading performance?
BarcodeReaderOptions allows users to tweak or adjust the behavior of the barcode reader to address all these issues. For detailed examples on setting barcode reader options, check our comprehensive guide. The following sections discuss all the adjustable properties available in BarcodeReaderOptions one by one.
How Do I Specify Which Area of the Image to Read?
CropArea is a property of type IronSoftware.Drawing.CropRectangle available in BarcodeReaderOptions that allows users to specify the area in an image IronBarcode should read. This helps improve reading performance, since the barcode reader does not scan through the entire image for barcodes, as well as improving reading accuracy since the area of read has been specified. Learn more about how to specify crop regions for optimal performance.
To set the CropArea property, simply instantiate a new Rectangle type object and specify the rectangle coordinates, width, and length of the rectangle as arguments. The measurement unit accepted is pixels (px).
// Example of setting CropArea
var cropArea = new IronSoftware.Drawing.Rectangle(x: 100, y: 100, width: 300, height: 300);
var options = new BarcodeReaderOptions()
{
CropArea = cropArea
};Imports IronSoftware.Drawing
' Example of setting CropArea
Dim cropArea As New Rectangle(x:=100, y:=100, width:=300, height:=300)
Dim options As New BarcodeReaderOptions() With {
.CropArea = cropArea
}Which Barcode Types Should I Specify for Faster Reading?
By default, all supported barcodes in IronBarcode will be scanned in an image. However, if the user knows what types of barcodes are available or wants to be read in an image, setting this property to read only certain types of barcodes will greatly increase reading performance and accuracy since the barcode reader does not need to iterate through collections of barcodes to interpret and read a barcode.
To use this property, simply set the ExpectBarcodeTypes to one of the fields of the BarcodeEncoding enum. Below are examples of every barcode type supported by IronBarcode.
// Example: Expect only QR codes and Code128
var options = new BarcodeReaderOptions()
{
ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128
};' Example: Expect only QR codes and Code128
Dim options As New BarcodeReaderOptions() With {
.ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128
}When Should I Disable Multiple Barcode Reading?
IronBarcode will scan all barcodes available in an image by default, which includes scanning the whole image file and adding the read barcode values into the string array. However, if users do not wish to read multiple barcodes in an image file, users can set this property to false, which will make the barcode reader stop scanning once a barcode value has been found. This will again, improve the performance and reading speed of IronBarcode. For more information on reading multiple barcodes, see our dedicated guide.
How Do Image Filters Improve Barcode Recognition?
One of the properties that can be added in BarcodeReaderOptions is a collection of image filters. Image filters are important for preprocessing the raw image fed to IronBarcode. To apply image filters inside the BarcodeReaderOptions, users must first initiate and specify the ImageFilter collection to be used. For comprehensive guidance on image correction techniques, including filter applications, visit our tutorial.
How Can I Optimize Threading for Better Performance?
IronBarcode allows users to enable and tweak the amount of parallel threads execution, which in turn will improve the speed and efficiency of the process. Parallel threads mean execution of multiple threads simultaneously on different processor cores. The default amount for the MaxParallelThreads property in IronBarcode is 4. Users can adjust them based on the capabilities and amount of resources their machines have.
Should I Enable Multithreaded Processing?
This property enables IronBarcode to read multiple images in parallel. The default for #Multithreaded is true, hence the multiple threads will be managed automatically to improve performance for batch barcode reading tasks.
Why Should I Remove False Positive Readings?
This property removes any false-positive barcode reads. False-positive barcode reads simply mean a false read of barcode values, but identified as valid. This can happen due to errors in the sequencing process or errors in the barcode labeling or preparation process. Therefore, setting RemoveFalsePositive as true will remove the false-positive barcode readings, thus improving barcode reading accuracy. However, if users opt for performance at the cost of accuracy, setting this property to false would help. The default value for this property is true.
How Does MinScanLines Affect Detection Accuracy?
MinScanLines sets the minimum number of scan lines that must agree on a barcode result for it to be considered valid. The default is 2. Increasing this value reduces false positives but may prevent detection of thin or low-quality barcodes. Lowering it to 1 increases sensitivity for difficult barcodes at the risk of introducing noise.
What Scan Modes Are Available for Different Use Cases?
Define how IronBarcode scans and detects barcodes in an image.
Auto: Reads barcodes with automatic image pre-processing and the most optimal reader options configured. Recommended for best results and performance.OnlyDetectionModel: Scans the image for barcodes and returns their positions as an array ofIronSoftware.Drawing.PointF. This mode does not read the detected barcodes; it only returns the positions of each barcode.MachineLearningScan: Scans the image for barcodes with machine learning detection and reads them.OnlyBasicScan: Reads barcodes without machine learning detection, automatic image pre-processing, or reader option configuration. This option can be used withIronBarCode.Slimalone.
How Do Reading Speed Settings Affect Accuracy?
As the name suggests, the Speed property enables users to further optimize the performance of the IronBarcode reader. Similar to the RemoveFalsePositive property, adjusting this property improves performance at the cost of accuracy. For an in-depth look at reading speed options, including performance benchmarks, see our detailed guide. It accepts the ReadingSpeed enum, which has 4 levels as shown below:
Faster: Enables the fastest barcode reading but reduces accuracy. The process skips image preprocessing, often resulting in empty barcode results. Use this setting only if the input image is sharp and clear.Balanced: This setting is recommended for theSpeedproperty. It sets a balance between accuracy and read performance by attempting to apply light processing to the image to clarify the barcode area and make it stand out for the barcode reader to detect. Most of the time, this setting is enough for IronBarcode to read a barcode image and produce accurate output.Detailed: For cases where using the settingReadingSpeed.Balancedis not successful in producing barcode values from the read, users may opt to useReadingSpeed.Detailed. IronBarcode will perform medium processing on the image to clarify the barcode area further and clearer for the barcode reader to detect the barcode. This setting is very useful to detect a small or less sharp barcode image.ExtremeDetail: This setting is the least recommended due to its CPU-intensive process. Heavy processing will be performed on the barcode image in order for the reader to read the barcodes. Users are advised to do image preprocessing/applying filters on the image before opting for this setting.
When Should I Use Code39 Extended Mode?
This setting allows Code39 type barcodes to be read and interpreted with extended mode whereby the full ASCII Character Set will be applied. Setting UseCode39ExtendedMode to true will enable a more accurate reading of Code39 barcodes.
How Do I Implement Advanced Barcode Reading with Custom Options?
Now that we have learned all the options that can be tweaked by users, be it to increase performance or accuracy, here's how to apply them in code. The code snippet below demonstrates comprehensive usage of BarcodeReaderOptions:
using IronBarCode;
using IronSoftware.Drawing;
using System;
using System.Linq;
// Create custom reader options
var options = new BarcodeReaderOptions()
{
// Specify expected barcode types for better performance
ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128 | BarcodeEncoding.Code39,
// Define specific area to scan (x, y, width, height in pixels)
CropArea = new Rectangle(100, 100, 500, 400),
// Set reading speed to balance accuracy and performance
Speed = ReadingSpeed.Balanced,
// Enable multithreading for better performance
Multithreaded = true,
MaxParallelThreads = 4,
// Remove false positives for accuracy
RemoveFalsePositive = true,
// Minimum scan lines that must agree for a valid result (default 2)
MinScanLines = 1,
// Enable Code39 extended mode if needed
UseCode39ExtendedMode = true,
// Set scan mode
ScanMode = BarcodeScanMode.Auto,
// Add image filters for better recognition
ImageFilters = new ImageFilterCollection() {
new SharpenFilter(),
new InvertFilter(),
new ContrastFilter()
}
};
// Read barcodes with custom options
var results = BarcodeReader.Read(@"C:\path\to\your\barcode-image.png", options);
// Process results
if (results.Any())
{
foreach (var barcode in results)
{
Console.WriteLine($"Barcode Type: {barcode.BarcodeType}");
Console.WriteLine($"Value: {barcode.Value}");
Console.WriteLine($"Position: {string.Join(", ", barcode.Points.Select(p => $"({p.X}, {p.Y})"))}");
Console.WriteLine("---");
}
}
else
{
Console.WriteLine("No barcodes found in the image.");
}
From the code snippet, we see that to use BarcodeReaderOptions we have to first initialize it, then determine and adjust the properties of the BarcodeReaderOptions according to the properties stated above. The initialized BarcodeReaderOptions can then be used as an argument in the BarcodeReader.Read() method along with the image file. This will apply all the settings in BarcodeReaderOptions when reading a barcode from the image.
If you encounter issues where your barcode is not recognized, our troubleshooting guide provides solutions for common problems and tips to enhance barcode scanning accuracy.
Frequently Asked Questions
How can I read barcodes from images using IronBarcode in C#?
You can read barcodes from images in C# with IronBarcode by using the `BarcodeReader.Read()` method. This method simplifies barcode reading with a single line of code and supports various image formats such as PNG, JPEG, GIF, BMP, and TIFF.
What image formats does IronBarcode support for barcode reading?
IronBarcode supports multiple image formats for barcode reading, including PNG, JPEG, GIF, BMP, TIFF, and SVG.
How do I customize the barcode reading options in IronBarcode?
You can customize barcode reading options in IronBarcode using the `BarcodeReaderOptions` class. It allows you to configure settings like `CropArea`, `ExpectBarcodeTypes`, `Speed`, and more to improve performance and accuracy.
Can IronBarcode read both 1D and 2D barcodes?
Yes, IronBarcode can read both 1D and 2D barcodes. Supported formats include Codabar, Code128, QRCode, DataMatrix, and more.
What is the purpose of the `CropArea` property in IronBarcode?
The `CropArea` property in IronBarcode is used to specify the specific area of an image where barcodes should be read. This helps enhance reading performance and accuracy by focusing on a defined region.
How can I increase the reading speed of barcodes with IronBarcode?
You can increase the barcode reading speed in IronBarcode by adjusting the `Speed` property in `BarcodeReaderOptions`. This allows you to balance performance and accuracy depending on your needs.
What are the benefits of using multithreading in IronBarcode?
Multithreading in IronBarcode allows multiple barcodes to be processed simultaneously, improving the speed and efficiency of barcode reading, especially in batch processing tasks.
How do image filters improve barcode recognition in IronBarcode?
Image filters in IronBarcode enhance preprocessing by improving the clarity of barcode images. Filters like the `SharpenFilter` and `ContrastFilter` help optimize image quality for better recognition.
What is the `MinScanLines` property used for in IronBarcode?
The `MinScanLines` property sets the minimum number of scan lines that must agree on a barcode result for it to be considered valid, reducing false positives while ensuring detection accuracy.
How do I handle multiple barcode types with IronBarcode?
In IronBarcode, you can specify which barcode types to read by setting the `ExpectBarcodeTypes` property in `BarcodeReaderOptions`, which improves performance by focusing on specific barcode types.
