How to Read Barcodes From System.Drawing in C#
IronBarcode enables reading barcodes from System.Drawing objects on all operating systems by automatically converting them to AnyBitmap through IronDrawing, solving Microsoft's Windows-only limitation for System.Drawing support.
Introduction
System.Drawing objects are widely used in .NET for image processing tasks. However, Microsoft has discontinued support for System.Drawing on MacOS and Linux, now exclusively supporting Windows. This change has created issues for developers using IronBarcode on non-Windows operating systems, since working with barcodes typically involves graphics, images, and fonts.
To address this problem, we introduced IronDrawing. This free and open-source library, created by IronSoftware, simplifies cross-platform support and provides a seamless experience. When you install IronBarcode from NuGet, IronDrawing is automatically included in your project.
For developers new to barcode reading, see our comprehensive Reading Barcodes Tutorial covering fundamental concepts and basic usage patterns. If you're working with various image formats, our guide on reading barcodes from images provides additional context and examples.
Quickstart: Read a barcode using AnyBitmap in one easy line
This snippet shows how IronBarcode reads barcodes by creating a System.Drawing.Bitmap and letting IronDrawing implicitly cast it to AnyBitmap. With just one line, developers on any OS get fast results.
Get started making PDFs with NuGet now:
Install IronBarcode with NuGet Package Manager
Copy and run this code snippet.
var results = IronBarCode.BarcodeReader.Read((AnyBitmap)(new System.Drawing.Bitmap("yourImage.png")));Deploy to test on your live environment
Minimal Workflow (5 steps)
- Download the C# library for reading barcodes from
System.Drawing - Utilize IronDrawing to cast System.Drawing objects into AnyBitmap
- Use the
Readmethod to read barcodes from AnyBitmap objects - Display the detected barcode values on the console
- Explore another article to learn how IronDrawing is used for handling color and fonts
How Do I Cast System.Drawing Objects to AnyBitmap?
Reading barcodes from System.Drawing requires casting the object to AnyBitmap. IronDrawing was designed for ease of use and supports implicit casting for image objects from System.Drawing into IronSoftware.Drawing image objects called AnyBitmap.
Beyond System.Drawing objects, we support casting from other types:
System.Drawing.BitmapSystem.Drawing.ImageSkiaSharp.SKBitmapSkiaSharp.SKImageSixLabors.ImageSharp
See this code example for casting the above objects. Below demonstrates casting barcode images from System.Drawing objects into IronSoftware.Drawing.AnyBitmap:
Which System.Drawing Types Can Be Cast?
:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-system-drawing-cast-to-anybitmap.csusing IronSoftware.Drawing;
using System.Collections.Generic;
List<AnyBitmap> barcodes = new List<AnyBitmap>();
// Instantiate System.Drawing.Bitmap
System.Drawing.Bitmap bitmapFromBitmap = new System.Drawing.Bitmap("test1.jpg");
// Cast from System.Drawing.Bitmap to AnyBitmap
AnyBitmap barcode1 = bitmapFromBitmap;
barcodes.Add(barcode1);
// Instantiate System.Drawing.Bitmap
System.Drawing.Image bitmapFromFile = System.Drawing.Image.FromFile("test2.png");
// Cast from System.Drawing.Image to AnyBitmap
AnyBitmap barcode2 = bitmapFromFile;
barcodes.Add(barcode2);Imports IronSoftware.Drawing
Imports System.Collections.Generic
Private barcodes As New List(Of AnyBitmap)()
' Instantiate System.Drawing.Bitmap
Private bitmapFromBitmap As New System.Drawing.Bitmap("test1.jpg")
' Cast from System.Drawing.Bitmap to AnyBitmap
Private barcode1 As AnyBitmap = bitmapFromBitmap
barcodes.Add(barcode1)
' Instantiate System.Drawing.Bitmap
Dim bitmapFromFile As System.Drawing.Image = System.Drawing.Image.FromFile("test2.png")
' Cast from System.Drawing.Image to AnyBitmap
Dim barcode2 As AnyBitmap = bitmapFromFile
barcodes.Add(barcode2)This code demonstrates seamless integration between System.Drawing objects and IronBarcode through IronDrawing. This compatibility extends across various barcode formats, detailed in our supported barcode formats guide, including QR codes, Code 128, Code 39, and many others.
Why Does Implicit Casting Work?
In the code above, we loaded two barcode images as System.Drawing.Bitmap and System.Drawing.Image. We then implicitly cast them into AnyBitmap by assigning them to AnyBitmap objects, then added these objects to an AnyBitmap list.
IronDrawing's implicit casting mechanism uses operator overloading, providing transparent conversion between System.Drawing types and AnyBitmap. This design pattern lets developers maintain existing code while gaining cross-platform compatibility. The conversion preserves all image properties including resolution, color depth, and pixel data, ensuring no quality loss.
When Should I Use Explicit vs Implicit Casting?
While implicit casting provides convenience, explicit casting might be preferred in some scenarios:
// Implicit casting - clean and simple for straightforward conversions
System.Drawing.Bitmap systemBitmap = new System.Drawing.Bitmap("barcode.png");
AnyBitmap anyBitmap = systemBitmap; // Implicit cast
// Explicit casting - useful when type clarity is important
System.Drawing.Image systemImage = System.Drawing.Image.FromFile("qrcode.jpg");
AnyBitmap explicitBitmap = (AnyBitmap)systemImage; // Explicit cast
// When working with nullable types or conditional logic
System.Drawing.Bitmap? nullableBitmap = GetBitmapFromSource();
if (nullableBitmap != null)
{
AnyBitmap result = (AnyBitmap)nullableBitmap; // Explicit cast for clarity
// Process the barcode
}// Implicit casting - clean and simple for straightforward conversions
System.Drawing.Bitmap systemBitmap = new System.Drawing.Bitmap("barcode.png");
AnyBitmap anyBitmap = systemBitmap; // Implicit cast
// Explicit casting - useful when type clarity is important
System.Drawing.Image systemImage = System.Drawing.Image.FromFile("qrcode.jpg");
AnyBitmap explicitBitmap = (AnyBitmap)systemImage; // Explicit cast
// When working with nullable types or conditional logic
System.Drawing.Bitmap? nullableBitmap = GetBitmapFromSource();
if (nullableBitmap != null)
{
AnyBitmap result = (AnyBitmap)nullableBitmap; // Explicit cast for clarity
// Process the barcode
}IRON VB CONVERTER ERROR developers@ironsoftware.comWhat Are Common Casting Errors?
When converting System.Drawing to AnyBitmap, developers might encounter:
- Null Reference Exceptions: Verify your System.Drawing object isn't null before casting
- Unsupported Format Exceptions: Some exotic image formats require pre-conversion
- Memory Issues: Large images need proper disposal patterns
For troubleshooting casting issues, our troubleshooting guide provides solutions to common problems during barcode recognition.
How Do I Read Barcodes from AnyBitmap Objects?
IronBarcode accepts IronSoftware.Drawing.AnyBitmap objects in all methods without additional configuration. This simplifies development when using System.Drawing objects on non-Windows operating systems. The following code demonstrates this:
What Methods Accept AnyBitmap Parameters?
:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-system-drawing-read-anybitmap.csusing IronBarCode;
using IronSoftware.Drawing;
using System;
using System.Collections.Generic;
List<AnyBitmap> barcodes = new List<AnyBitmap>();
System.Drawing.Bitmap bitmapFromBitmap = new System.Drawing.Bitmap("test1.jpg");
AnyBitmap barcode1 = bitmapFromBitmap;
barcodes.Add(barcode1);
System.Drawing.Image bitmapFromFile = System.Drawing.Image.FromFile("test2.png");
AnyBitmap barcode2 = bitmapFromFile;
barcodes.Add(barcode2);
foreach (var barcode in barcodes)
{
// Read the barcode
var results = BarcodeReader.Read(barcode);
foreach (var result in results)
{
// Output the detected barcode value
Console.WriteLine(result.Value);
}
}Imports IronBarCode
Imports IronSoftware.Drawing
Imports System
Imports System.Collections.Generic
Private barcodes As New List(Of AnyBitmap)()
Private bitmapFromBitmap As New System.Drawing.Bitmap("test1.jpg")
Private barcode1 As AnyBitmap = bitmapFromBitmap
barcodes.Add(barcode1)
Dim bitmapFromFile As System.Drawing.Image = System.Drawing.Image.FromFile("test2.png")
Dim barcode2 As AnyBitmap = bitmapFromFile
barcodes.Add(barcode2)
For Each barcode In barcodes
' Read the barcode
Dim results = BarcodeReader.Read(barcode)
For Each result In results
' Output the detected barcode value
Console.WriteLine(result.Value)
Next result
Next barcodeBeyond the basic Read method, IronBarcode provides several methods accepting AnyBitmap parameters. For advanced scenarios, see our guide on reading multiple barcodes demonstrating efficient processing of multiple barcodes in a single image:
// Advanced barcode reading with options
var readerOptions = new BarcodeReaderOptions
{
// Specify barcode types to search for
ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128,
// Enable machine learning for better accuracy
UseML = true,
// Set confidence threshold
Confidence = 0.95
};
// Read with specific options
var advancedResults = BarcodeReader.Read(anyBitmap, readerOptions);// Advanced barcode reading with options
var readerOptions = new BarcodeReaderOptions
{
// Specify barcode types to search for
ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128,
// Enable machine learning for better accuracy
UseML = true,
// Set confidence threshold
Confidence = 0.95
};
// Read with specific options
var advancedResults = BarcodeReader.Read(anyBitmap, readerOptions);IRON VB CONVERTER ERROR developers@ironsoftware.comHow Do I Handle Multiple Barcode Results?
The code above extends the previous example. After populating the AnyBitmap list, we iterated through it and called the Read method on each AnyBitmap object, which returned IronBarcode.BarcodeResults. We then iterated through the results to print barcode values to the console.
When handling multiple barcodes, leverage parallel processing for better performance:
// Parallel processing for multiple barcode images
var barcodeFiles = Directory.GetFiles("barcodes/", "*.png");
var allResults = new ConcurrentBag<BarcodeResult>();
Parallel.ForEach(barcodeFiles, file =>
{
var bitmap = new System.Drawing.Bitmap(file);
var anyBitmap = (AnyBitmap)bitmap;
var results = BarcodeReader.Read(anyBitmap);
foreach (var result in results)
{
allResults.Add(result);
}
bitmap.Dispose(); // Clean up resources
});
// Process all results
foreach (var result in allResults)
{
Console.WriteLine($"Found {result.BarcodeType}: {result.Value}");
}// Parallel processing for multiple barcode images
var barcodeFiles = Directory.GetFiles("barcodes/", "*.png");
var allResults = new ConcurrentBag<BarcodeResult>();
Parallel.ForEach(barcodeFiles, file =>
{
var bitmap = new System.Drawing.Bitmap(file);
var anyBitmap = (AnyBitmap)bitmap;
var results = BarcodeReader.Read(anyBitmap);
foreach (var result in results)
{
allResults.Add(result);
}
bitmap.Dispose(); // Clean up resources
});
// Process all results
foreach (var result in allResults)
{
Console.WriteLine($"Found {result.BarcodeType}: {result.Value}");
}IRON VB CONVERTER ERROR developers@ironsoftware.comWhat Other IronDrawing Features Can I Use?
IronSoftware.Drawing functionality extends beyond casting images. It handles image processing aspects like colors and fonts useful for styling barcodes and QR codes. Explore how we utilize IronDrawing to customize and add logos to QR codes.
IronDrawing provides powerful image manipulation capabilities complementing barcode processing:
// Using IronDrawing for image preprocessing
using IronSoftware.Drawing;
// Load and preprocess an image before barcode reading
AnyBitmap preprocessedImage = AnyBitmap.FromFile("noisy-barcode.jpg");
// Apply image filters to improve barcode readability
preprocessedImage = preprocessedImage.ToGrayScale();
preprocessedImage = preprocessedImage.Contrast(1.5); // Increase contrast
preprocessedImage = preprocessedImage.Sharpen(); // Sharpen image
// Read the preprocessed barcode
var improvedResults = BarcodeReader.Read(preprocessedImage);// Using IronDrawing for image preprocessing
using IronSoftware.Drawing;
// Load and preprocess an image before barcode reading
AnyBitmap preprocessedImage = AnyBitmap.FromFile("noisy-barcode.jpg");
// Apply image filters to improve barcode readability
preprocessedImage = preprocessedImage.ToGrayScale();
preprocessedImage = preprocessedImage.Contrast(1.5); // Increase contrast
preprocessedImage = preprocessedImage.Sharpen(); // Sharpen image
// Read the preprocessed barcode
var improvedResults = BarcodeReader.Read(preprocessedImage);IRON VB CONVERTER ERROR developers@ironsoftware.comFor scenarios requiring specific image corrections, our image correction guide details using filters to enhance barcode readability.
Why Choose IronDrawing Over System.Drawing?
IronDrawing offers compelling advantages over System.Drawing:
- Cross-Platform Support: Works seamlessly on Windows, Linux, and macOS unlike System.Drawing (Windows-only in .NET Core/5+)
- Modern Architecture: Built on SkiaSharp and ImageSharp for better performance and memory management
- Simplified API: Maintains familiar System.Drawing-like interfaces while adding modern conveniences
- Active Development: Regular updates and improvements, unlike System.Drawing in maintenance mode
- Better Integration: Designed specifically for optimal performance with IronSoftware products
For deployment considerations, especially for cloud environments, see our guides on deploying to Azure and deploying to AWS, which include specific notes about cross-platform compatibility using IronDrawing.
Whether building desktop applications, web services, or cloud-native solutions, IronDrawing ensures your barcode processing code remains portable and efficient across all platforms, making it the ideal choice for modern .NET development.
Frequently Asked Questions
How can I read barcodes from System.Drawing objects on non-Windows platforms?
IronBarcode automatically handles cross-platform barcode reading from System.Drawing objects through IronDrawing, which converts them to AnyBitmap format. This solves Microsoft's limitation of System.Drawing being Windows-only, allowing you to read barcodes on MacOS and Linux systems seamlessly.
What is IronDrawing and why is it included with barcode reading?
IronDrawing is a free, open-source library created by IronSoftware that provides cross-platform support for graphics operations. It's automatically included when you install IronBarcode from NuGet and enables barcode reading from System.Drawing objects on all operating systems by converting them to the compatible AnyBitmap format.
How do I convert a System.Drawing.Bitmap to read barcodes from it?
You can read barcodes from System.Drawing.Bitmap with a simple cast to AnyBitmap: `var results = BarcodeReader.Read((AnyBitmap)(new System.Drawing.Bitmap("yourImage.png")));`. IronBarcode handles the conversion automatically through IronDrawing's implicit casting feature.
Can I read barcodes on Linux and MacOS using System.Drawing?
Yes, IronBarcode enables barcode reading from System.Drawing objects on Linux and MacOS through IronDrawing, which automatically converts System.Drawing objects to the cross-platform AnyBitmap format. This overcomes Microsoft's Windows-only limitation for System.Drawing support.
What types of System.Drawing objects can be used for barcode reading?
IronBarcode supports barcode reading from various System.Drawing objects including System.Drawing.Bitmap and other image types. These are automatically converted to AnyBitmap through IronDrawing's implicit casting feature, enabling cross-platform barcode scanning functionality.
Is there a simple one-line solution to read barcodes from System.Drawing?
Yes, IronBarcode provides a one-line solution: `var results = BarcodeReader.Read((AnyBitmap)(new System.Drawing.Bitmap("yourImage.png")));`. This single line creates a System.Drawing.Bitmap, casts it to AnyBitmap through IronDrawing, and reads all barcodes present in the image.






