C# Barcode Scanner: Read Barcodes & QR Codes in .NET Applications
Need to quickly scan barcodes or QR codes in your .NET application? IronBarcode makes barcode reading simple and reliable, whether you're processing perfect digital images or challenging real-world photos. This guide shows you exactly how to implement barcode scanning in C# with practical examples you can use immediately.
Quickstart: Read a Barcode from a File Instantly
This quick example shows you how easy it is to get started with IronBarcode. In just one line of code, you can read barcodes from an image file—no complex setup required.
Get started making PDFs with NuGet now:
Install IronBarcode with NuGet Package Manager
Copy and run this code snippet.
var results = IronBarCode.BarcodeReader.Read("path/to/barcode.png");Deploy to test on your live environment
Minimal Workflow (5 steps)
- Install IronBarcode from NuGet or via DLL download
- Use the
BarcodeReader.Readmethod to scan any barcode or QR code - Read multiple barcodes or QR codes in a single scan, PDF, or multiframe TIFF file
- Enable IronBarcode to decode imperfect scans and photos with advanced filters
- Download the tutorial project and start scanning immediately
How do I install IronBarcode in my .NET project?
IronBarcode installs easily through NuGet Package Manager or by downloading the DLL directly. The NuGet installation is the recommended approach as it automatically manages dependencies and updates.
Install-Package BarCode
After installation, add using IronBarCode; to your C# files to access the barcode scanning functionality. For detailed installation instructions across different development environments, check our installation guide.
How can I read my first barcode using C#?
Reading barcodes with IronBarcode requires just one line of code. The library automatically detects barcode formats and extracts all encoded data.
A standard Code128 barcode that IronBarcode can read instantlyusing IronBarCode;
using System;
// Read barcodes from the image file - supports PNG, JPG, BMP, GIF, and more
BarcodeResults results = BarcodeReader.Read("GetStarted.png");
// Check if any barcodes were detected
if (results != null && results.Count > 0)
{
// Process each barcode found in the image
foreach (BarcodeResult result in results)
{
// Extract the text value from the barcode
Console.WriteLine("Barcode detected! Value: " + result.Text);
// Additional properties available:
// result.BarcodeType - The format (Code128, QR, etc.)
// result.BinaryValue - Raw binary data if applicable
// result.Confidence - Detection confidence score
}
}
else
{
Console.WriteLine("No barcodes detected in the image.");
}using IronBarCode;
using System;
// Read barcodes from the image file - supports PNG, JPG, BMP, GIF, and more
BarcodeResults results = BarcodeReader.Read("GetStarted.png");
// Check if any barcodes were detected
if (results != null && results.Count > 0)
{
// Process each barcode found in the image
foreach (BarcodeResult result in results)
{
// Extract the text value from the barcode
Console.WriteLine("Barcode detected! Value: " + result.Text);
// Additional properties available:
// result.BarcodeType - The format (Code128, QR, etc.)
// result.BinaryValue - Raw binary data if applicable
// result.Confidence - Detection confidence score
}
}
else
{
Console.WriteLine("No barcodes detected in the image.");
}The BarcodeReader.Read method returns a BarcodeResults collection containing all detected barcodes. Each BarcodeResult provides access to the barcode's text value, format type, position coordinates, and binary data. This approach works seamlessly with common barcode formats including Code128, Code39, QR codes, and Data Matrix codes.
What options help read challenging or damaged barcodes?
Real-world barcode scanning often involves imperfect images - skewed angles, poor lighting, or partial damage. IronBarcode's advanced options handle these challenges effectively.
using IronBarCode;
// Configure advanced reading options for difficult barcodes
BarcodeReaderOptions options = new BarcodeReaderOptions
{
// Speed settings: Faster, Balanced, Detailed, ExtremeDetail
// ExtremeDetail performs deep analysis for challenging images
Speed = ReadingSpeed.ExtremeDetail,
// Specify expected formats to improve performance
// Use bitwise OR (|) to combine multiple formats
ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128,
// Maximum number of barcodes to find (0 = unlimited)
MaxParallelThreads = 4,
// Crop region for faster processing of specific areas
CropArea = null // Or specify a Rectangle
};
// Apply options when reading
BarcodeResults results = BarcodeReader.Read("TryHarderQR.png", options);
// Process detected barcodes
foreach (var barcode in results)
{
Console.WriteLine($"Format: {barcode.BarcodeType}, Value: {barcode.Text}");
}using IronBarCode;
// Configure advanced reading options for difficult barcodes
BarcodeReaderOptions options = new BarcodeReaderOptions
{
// Speed settings: Faster, Balanced, Detailed, ExtremeDetail
// ExtremeDetail performs deep analysis for challenging images
Speed = ReadingSpeed.ExtremeDetail,
// Specify expected formats to improve performance
// Use bitwise OR (|) to combine multiple formats
ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128,
// Maximum number of barcodes to find (0 = unlimited)
MaxParallelThreads = 4,
// Crop region for faster processing of specific areas
CropArea = null // Or specify a Rectangle
};
// Apply options when reading
BarcodeResults results = BarcodeReader.Read("TryHarderQR.png", options);
// Process detected barcodes
foreach (var barcode in results)
{
Console.WriteLine($"Format: {barcode.BarcodeType}, Value: {barcode.Text}");
}
A rotated QR code that IronBarcode successfully reads using advanced optionsThe ExpectBarcodeTypes property significantly improves performance by limiting the search to specific formats. For maximum accuracy with problematic images, combine image filters with automatic rotation:
using IronBarCode;
BarcodeReaderOptions options = new BarcodeReaderOptions
{
// Apply image processing filters to enhance readability
ImageFilters = new ImageFilterCollection
{
new AdaptiveThresholdFilter(9, 0.01f), // Handles varying lighting
new ContrastFilter(2.0f), // Increases contrast
new SharpenFilter() // Reduces blur
},
// Automatically rotate to find barcodes at any angle
AutoRotate = true,
// Use multiple CPU cores for faster processing
Multithreaded = true
};
BarcodeResults results = BarcodeReader.Read("TryHarderQR.png", options);
foreach (var result in results)
{
Console.WriteLine($"Detected {result.BarcodeType}: {result.Text}");
Console.WriteLine($"Confidence: {result.Confidence}%");
Console.WriteLine($"Position: X={result.X}, Y={result.Y}");
}using IronBarCode;
BarcodeReaderOptions options = new BarcodeReaderOptions
{
// Apply image processing filters to enhance readability
ImageFilters = new ImageFilterCollection
{
new AdaptiveThresholdFilter(9, 0.01f), // Handles varying lighting
new ContrastFilter(2.0f), // Increases contrast
new SharpenFilter() // Reduces blur
},
// Automatically rotate to find barcodes at any angle
AutoRotate = true,
// Use multiple CPU cores for faster processing
Multithreaded = true
};
BarcodeResults results = BarcodeReader.Read("TryHarderQR.png", options);
foreach (var result in results)
{
Console.WriteLine($"Detected {result.BarcodeType}: {result.Text}");
Console.WriteLine($"Confidence: {result.Confidence}%");
Console.WriteLine($"Position: X={result.X}, Y={result.Y}");
}These advanced features make IronBarcode ideal for scanning barcodes from photos, security cameras, or mobile device captures where image quality varies significantly.
How do I scan multiple barcodes from PDF documents?
PDF barcode scanning is essential for processing invoices, shipping labels, and inventory documents. IronBarcode reads all barcodes across every page efficiently.
Reading barcodes from PDF files
using System;
using IronBarCode;
try
{
// Scan all pages of a PDF for barcodes
BarcodeResults results = BarcodeReader.ReadPdf("MultipleBarcodes.pdf");
if (results != null && results.Count > 0)
{
foreach (var barcode in results)
{
// Access barcode data and metadata
string value = barcode.Text;
int pageNumber = barcode.PageNumber;
BarcodeEncoding format = barcode.BarcodeType;
byte[] binaryData = barcode.BinaryValue;
// Extract barcode image if needed
System.Drawing.Bitmap barcodeImage = barcode.BarcodeImage;
Console.WriteLine($"Found {format} on page {pageNumber}: {value}");
}
}
else
{
Console.WriteLine("No barcodes found in the PDF.");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error reading PDF: {ex.Message}");
}using System;
using IronBarCode;
try
{
// Scan all pages of a PDF for barcodes
BarcodeResults results = BarcodeReader.ReadPdf("MultipleBarcodes.pdf");
if (results != null && results.Count > 0)
{
foreach (var barcode in results)
{
// Access barcode data and metadata
string value = barcode.Text;
int pageNumber = barcode.PageNumber;
BarcodeEncoding format = barcode.BarcodeType;
byte[] binaryData = barcode.BinaryValue;
// Extract barcode image if needed
System.Drawing.Bitmap barcodeImage = barcode.BarcodeImage;
Console.WriteLine($"Found {format} on page {pageNumber}: {value}");
}
}
else
{
Console.WriteLine("No barcodes found in the PDF.");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error reading PDF: {ex.Message}");
}
Console output showing multiple barcodes found across different PDF pages
For specific page ranges or advanced PDF processing, use BarcodeReaderOptions:
// Read only specific pages to improve performance
BarcodeReaderOptions pdfOptions = new BarcodeReaderOptions
{
// Scan pages 1-5 only
PageNumbers = new[] { 1, 2, 3, 4, 5 },
// PDF-specific settings
PdfDpi = 300, // Higher DPI for better accuracy
ReadBehindVectorGraphics = true
};
BarcodeResults results = BarcodeReader.ReadPdf("document.pdf", pdfOptions);// Read only specific pages to improve performance
BarcodeReaderOptions pdfOptions = new BarcodeReaderOptions
{
// Scan pages 1-5 only
PageNumbers = new[] { 1, 2, 3, 4, 5 },
// PDF-specific settings
PdfDpi = 300, // Higher DPI for better accuracy
ReadBehindVectorGraphics = true
};
BarcodeResults results = BarcodeReader.ReadPdf("document.pdf", pdfOptions);How can I process multiframe TIFF images?
Multiframe TIFF files, common in document scanning and fax systems, receive the same comprehensive support as PDFs.
A multiframe TIFF file with barcodes on different frames
using IronBarCode;
// TIFF files are processed similarly to regular images
// Each frame is scanned automatically
BarcodeResults multiFrameResults = BarcodeReader.Read("Multiframe.tiff");
foreach (var result in multiFrameResults)
{
// Access frame-specific information
int frameNumber = result.PageNumber; // Frame number in TIFF
string barcodeValue = result.Text;
Console.WriteLine($"Frame {frameNumber}: {barcodeValue}");
// Save individual barcode images if needed
result.BarcodeImage?.Save($"barcode_frame_{frameNumber}.png");
}using IronBarCode;
// TIFF files are processed similarly to regular images
// Each frame is scanned automatically
BarcodeResults multiFrameResults = BarcodeReader.Read("Multiframe.tiff");
foreach (var result in multiFrameResults)
{
// Access frame-specific information
int frameNumber = result.PageNumber; // Frame number in TIFF
string barcodeValue = result.Text;
Console.WriteLine($"Frame {frameNumber}: {barcodeValue}");
// Save individual barcode images if needed
result.BarcodeImage?.Save($"barcode_frame_{frameNumber}.png");
}The same BarcodeReaderOptions apply to TIFF processing, including image filters and rotation settings. For detailed TIFF handling scenarios, see our image processing tutorials.
Can I speed up processing with multithreading?
Processing multiple documents benefits dramatically from parallel processing. IronBarcode automatically utilizes available CPU cores for optimal performance.
using IronBarCode;
// List of documents to process - mix of formats supported
var documentBatch = new[]
{
"invoice1.pdf",
"shipping_label.png",
"inventory_sheet.tiff",
"product_catalog.pdf"
};
// Configure for batch processing
BarcodeReaderOptions batchOptions = new BarcodeReaderOptions
{
// Enable parallel processing across documents
Multithreaded = true,
// Limit threads if needed (0 = use all cores)
MaxParallelThreads = Environment.ProcessorCount,
// Apply consistent settings to all documents
Speed = ReadingSpeed.Balanced,
ExpectBarcodeTypes = BarcodeEncoding.All
};
// Process all documents in parallel
BarcodeResults batchResults = BarcodeReader.Read(documentBatch, batchOptions);
// Group results by source document
var resultsByDocument = batchResults.GroupBy(r => r.Filename);
foreach (var docGroup in resultsByDocument)
{
Console.WriteLine($"\nDocument: {docGroup.Key}");
foreach (var barcode in docGroup)
{
Console.WriteLine($" - {barcode.BarcodeType}: {barcode.Text}");
}
}using IronBarCode;
// List of documents to process - mix of formats supported
var documentBatch = new[]
{
"invoice1.pdf",
"shipping_label.png",
"inventory_sheet.tiff",
"product_catalog.pdf"
};
// Configure for batch processing
BarcodeReaderOptions batchOptions = new BarcodeReaderOptions
{
// Enable parallel processing across documents
Multithreaded = true,
// Limit threads if needed (0 = use all cores)
MaxParallelThreads = Environment.ProcessorCount,
// Apply consistent settings to all documents
Speed = ReadingSpeed.Balanced,
ExpectBarcodeTypes = BarcodeEncoding.All
};
// Process all documents in parallel
BarcodeResults batchResults = BarcodeReader.Read(documentBatch, batchOptions);
// Group results by source document
var resultsByDocument = batchResults.GroupBy(r => r.Filename);
foreach (var docGroup in resultsByDocument)
{
Console.WriteLine($"\nDocument: {docGroup.Key}");
foreach (var barcode in docGroup)
{
Console.WriteLine($" - {barcode.BarcodeType}: {barcode.Text}");
}
}This parallel approach processes documents simultaneously, reducing total scanning time by up to 75% on multicore systems. For enterprise-scale barcode processing, explore our performance optimization guide.
Summary
IronBarcode transforms complex barcode scanning into straightforward C# code. Whether you're building inventory systems, document processors, or mobile applications, the library handles everything from pristine digital barcodes to challenging real-world captures.
Key capabilities covered:
- Single-line barcode reading from images
- Advanced options for damaged or rotated barcodes
- Comprehensive PDF and TIFF document scanning
- High-performance batch processing with multithreading
- Support for all major barcode formats
Further Reading
Expand your barcode processing capabilities with these resources:
- Barcode Generation Tutorial - Create custom barcodes
- QR Code Guide](/csharp/barcode/tutorials/csharp-qr-code-generator/) - Specialized QR code features
BarcodeReaderClass Reference - Complete API documentation- Troubleshooting Guide - Common issues and solutions
Source Code Downloads
Run these examples yourself:
Ready to implement barcode scanning in your application? Start your free trial and add professional barcode reading to your .NET project today.
Frequently Asked Questions
How can I install a barcode reading library in a .NET project?
You can install the IronBarcode library via NuGet Package Manager using the command dotnet add package BarCode or through Visual Studio's NuGet interface. Alternatively, download the DLL for manual installation.
What is the method to read a barcode from an image using C#?
Use the BarcodeReader.Read method from IronBarcode with a single line of code: var results = BarcodeReader.Read('image.png'); This method detects and reads all barcode formats present in the image.
Is it possible to detect multiple barcodes in a single image or document?
Yes, IronBarcode can automatically detect and read multiple barcodes in an image, PDF, or multiframe TIFF, returning each barcode's value, type, and position in a BarcodeResults collection.
How do I read barcodes from a PDF using C#?
Use IronBarcode's BarcodeReader.ReadPdf method to scan all pages of a PDF document: var results = BarcodeReader.ReadPdf('document.pdf'); Each result includes the page number where the barcode was found.
What should I do if the barcode images are blurry or rotated?
Configure BarcodeReaderOptions to handle challenging images by setting AutoRotate = true and applying image filters like SharpenFilter or AdaptiveThresholdFilter. Use Speed = ExtremeDetail for better accuracy.
Which barcode formats are supported in .NET applications?
IronBarcode supports all major barcode formats such as QR Code, Code 128, Code 39, EAN-13, UPC-A, Data Matrix, PDF417, and more. Utilize BarcodeEncoding.All to scan for any supported format.
How can I enhance barcode scanning performance in a C# application?
Improve performance by specifying expected barcode types with ExpectBarcodeTypes, enabling Multithreaded processing, and choosing appropriate Speed settings. For batch tasks, utilize BarcodeReader.Read with file paths.
What is the recommended approach for handling barcode reading errors?
Encapsulate barcode reading in try-catch blocks and verify if the results are null or empty. IronBarcode provides detailed error messages and a Confidence property to indicate detection reliability.
Can I extract barcode images after they are scanned?
Yes, IronBarcode's BarcodeResult includes a BarcodeImage property that contains a Bitmap of the detected barcode, which can be saved or processed separately.
How do I read barcodes from specific pages within a PDF document?
Set the PageNumbers property in BarcodeReaderOptions to specify pages: options.PageNumbers = new[] {1, 2, 3}; This optimizes performance by scanning only the designated pages.
What image formats are compatible with barcode scanning in .NET?
IronBarcode supports scanning in formats like PNG, JPEG, BMP, GIF, TIFF (including multiframe), and PDF. You can load images from file paths, streams, or byte arrays.
How can I access binary data from scanned barcodes in C#?
Utilize the BinaryValue property of BarcodeResult to obtain raw binary data, especially useful for barcodes containing non-text data such as compressed information or binary protocols.






