How to Read Barcodes From System.Drawing in C
IronBarcode 支持在所有操作系统上读取 System.Drawing 格式的 BARCODE,通过 IronDrawing 自动将其转换为 AnyBitmap 格式,从而解决了 Microsoft 仅限 Windows 系统支持 System.Drawing 的限制。
简介
System.Drawing 对象在 .NET 中被广泛用于图像处理任务。 不过,微软已停止在 macOS 和 Linux 系统上对 System.Drawing 的支持,目前仅支持 Windows 系统。 这一变更给在非 Windows 操作系统上使用 IronBarcode 的开发者带来了困扰,因为条形码处理通常涉及图形、图像和字体。
为解决这一问题,我们推出了 IronDrawing。 这款由 Iron Software 开发的免费开源库,简化了跨平台支持,并提供了无缝的使用体验。 当您从 NuGet 安装 IronBarcode 时,IronDrawing 会自动包含在您的项目中。
对于初次接触BarCode读取的开发者,请参阅我们的《BarCode读取综合教程》,其中涵盖了基础概念和基本使用模式。 如果您需要处理多种图像格式,我们的《从图像中读取BARCODE指南》提供了更多背景信息和示例。
快速入门:仅需一行代码即可使用 AnyBitmap 读取 BARCODE
此代码片段演示了 IronBarcode 如何通过创建 System.Drawing.Bitmap 并让 IronDrawing 将其隐式转换为 AnyBitmap 来读取条形码。 只需一行代码,任何操作系统的开发者都能快速获得结果。
简易工作流程(5个步骤)
- 下载用于读取 BarCode 的 C# 库
System.Drawing - 请使用
IronDrawing来将System.Drawing对象AnyBitmap - 使用
Read方法从BARCODEAnyBitmap对象 - 在控制台显示检测到的BarCode值
- 阅读另一篇文章,了解如何
IronDrawing用于处理颜色和字体
如何将 System.Drawing 对象转换为 AnyBitmap?
读取 System.Drawing 格式的 BARCODE 需要将对象强制转换为 AnyBitmap 格式。 IronDrawing 旨在提升易用性,并支持将 System.Drawing 中的图像对象隐式转换为名为 IronSoftware.Drawing 的图像对象。
除了 System.Drawing 对象外,我们还支持从其他类型进行类型转换:
System.Drawing.BitmapSystem.Drawing.ImageSkiaSharp.SKBitmapSkiaSharp.SKImageSixLabors.ImageSharp
请参阅此代码示例,了解如何对上述对象进行类型转换。 以下演示了如何将 System.Drawing 对象中的 BarCode 图像转换为 IronSoftware.Drawing.AnyBitmap:
哪些 System.Drawing 类型可以进行强制类型转换?
:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-system-drawing-cast-to-anybitmap.cs
using 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)
此代码演示了 System.Drawing 对象与 IronBarcode 通过 IronDrawing 实现的无缝集成。 这种兼容性涵盖多种 BARCODE 格式,详情请参阅我们的支持 BARCODE 格式指南,包括 QR 码、Code 128、Code 39 以及许多其他格式。
隐式类型转换为何有效?
在上述代码中,我们加载了两个BarCode图像,分别命名为 System.Drawing.Bitmap 和 System.Drawing.Image。 随后,我们通过将它们赋值给 AnyBitmap 对象,将其隐式转换为 AnyBitmap,然后将这些对象添加到 AnyBitmap 列表中。
IronDrawing 的隐式类型转换机制利用了运算符重载,可在 System.Drawing 类型与 AnyBitmap 之间实现透明转换。 该设计模式使开发人员能够在保持现有代码的同时,获得跨平台兼容性。 转换过程将保留所有图像属性,包括分辨率、色深和像素数据,确保无质量损失。
何时应使用显式转换与隐式转换?
虽然隐式类型转换提供了便利,但在某些情况下,显式类型转换可能更受青睐:
// 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
}
Imports System.Drawing
' Implicit casting - clean and simple for straightforward conversions
Dim systemBitmap As New Bitmap("barcode.png")
Dim anyBitmap As AnyBitmap = systemBitmap ' Implicit cast
' Explicit casting - useful when type clarity is important
Dim systemImage As Image = Image.FromFile("qrcode.jpg")
Dim explicitBitmap As AnyBitmap = CType(systemImage, AnyBitmap) ' Explicit cast
' When working with nullable types or conditional logic
Dim nullableBitmap As Bitmap = GetBitmapFromSource()
If nullableBitmap IsNot Nothing Then
Dim result As AnyBitmap = CType(nullableBitmap, AnyBitmap) ' Explicit cast for clarity
' Process the barcode
End If
常见的类型转换错误有哪些?
在将 System.Drawing 转换为 AnyBitmap 时,开发者可能会遇到:
- 空引用异常:在进行类型转换前,请先验证您的
System.Drawing对象是否为空 - 不支持的格式异常:某些罕见图像格式需要预先转换
- 内存问题:大图片需要正确的释放机制
针对类型转换问题,我们的故障排除指南提供了BarCode识别过程中常见问题的解决方案。
如何读取 AnyBitmap 对象中的 BarCode?
IronBarcode 在所有方法中均可直接接受 IronSoftware.Drawing.AnyBitmap 对象,无需额外配置。 这简化了在非 Windows 操作系统上使用 System.Drawing 对象时的开发工作。 以下代码对此进行了演示:
哪些方法接受 AnyBitmap 参数?
:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-system-drawing-read-anybitmap.cs
using IronBarCode;
using IronSoftware.Drawing;
using System;
using System.Collections.Generic;
// Create a list of image file paths to read barcodes from
List<string> barcodeFiles = new List<string>
{
"test1.jpg",
"test2.png"
};
foreach (var barcodeFile in barcodeFiles)
{
// Read the barcode from file path
var results = BarcodeReader.Read(barcodeFile);
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
' Create a list of image file paths to read barcodes from
Dim barcodeFiles As New List(Of String) From {
"test1.jpg",
"test2.png"
}
For Each barcodeFile In barcodeFiles
' Read the barcode from file path
Dim results = BarcodeReader.Read(barcodeFile)
For Each result In results
' Output the detected barcode value
Console.WriteLine(result.Value)
Next
Next
除了基本的 Read 方法外,IronBarcode 还提供了若干接受 AnyBitmap 参数的方法。 关于高级应用场景,请参阅我们的《读取多个BarCode指南》,该指南演示了如何高效处理单张图像中的多个BarCode:
// Advanced barcode reading with options
var readerOptions = new BarcodeReaderOptions
{
// Specify barcode types to search for
ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Co/de128,
// 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.Co/de128,
// 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
Dim readerOptions As New BarcodeReaderOptions With {
' Specify barcode types to search for
.ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128,
' Enable machine learning for better accuracy
.UseML = True,
' Set confidence threshold
.Confidence = 0.95
}
' Read with specific options
Dim advancedResults = BarcodeReader.Read(anyBitmap, readerOptions)
如何处理多个BarCode结果?
上面的代码是对前一个示例的扩展。 在填充 AnyBitmap 列表后,我们遍历该列表,并对每个 AnyBitmap 对象调用 Read 方法,该方法返回 IronBarCode.BarcodeResults。 随后,我们遍历了结果,将BarCode值打印到控制台。
处理多个BarCode时,请利用并行处理以获得更佳性能:
// 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}");
}
Imports System.IO
Imports System.Collections.Concurrent
Imports System.Drawing
Imports System.Threading.Tasks
' Parallel processing for multiple barcode images
Dim barcodeFiles = Directory.GetFiles("barcodes/", "*.png")
Dim allResults = New ConcurrentBag(Of BarcodeResult)()
Parallel.ForEach(barcodeFiles, Sub(file)
Dim bitmap = New Bitmap(file)
Dim anyBitmap = CType(bitmap, AnyBitmap)
Dim results = BarcodeReader.Read(anyBitmap)
For Each result In results
allResults.Add(result)
Next
bitmap.Dispose() ' Clean up resources
End Sub)
' Process all results
For Each result In allResults
Console.WriteLine($"Found {result.BarcodeType}: {result.Value}")
Next
我还可以使用哪些其他 IronDrawing 功能?
IronSoftware.Drawing 的功能不仅限于图像转换。 它处理图像处理方面的内容,例如颜色和字体,这些对于设计BARCODE和QR码非常有用。 了解我们如何利用 IronDrawing 自定义 QR 码并添加徽标。
IronDrawing 提供强大的图像处理功能,可与 BarCode 处理功能相辅相成:
// 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.Co/ntrast(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.Co/ntrast(1.5); // Increase contrast
preprocessedImage = preprocessedImage.Sharpen(); // Sharpen image
// Read the preprocessed barcode
var improvedResults = BarcodeReader.Read(preprocessedImage);
Imports IronSoftware.Drawing
' Load and preprocess an image before barcode reading
Dim preprocessedImage As AnyBitmap = 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
Dim improvedResults = BarcodeReader.Read(preprocessedImage)
对于需要进行特定图像校正的场景,我们的图像校正指南详细介绍了如何使用滤镜来提高BARCODE的可读性。
为何选择 IronDrawing 而不是 System.Drawing?
IronDrawing 相比 System.Drawing 具有显著优势:
- 跨平台支持:与
System.Drawing(仅限 .NET Core/5+ 下的 Windows 系统)不同,本工具可在 Windows、Linux 和 macOS 上无缝运行。 - 现代架构:基于
SkiaSharp和ImageSharp构建,以实现更优的性能和内存管理 - 简化 API:在保留类似
System.Drawing的熟悉接口的同时,增加了现代化的便捷功能 - 积极开发:定期更新和改进,不同于处于维护模式的
System.Drawing - 更佳的集成性:专为与 Iron Software产品实现最佳性能而设计
关于部署注意事项(尤其是云环境),请参阅我们的《Azure部署指南》和《AWS部署指南》,其中包含有关使用 IronDrawing 实现跨平台兼容性的具体说明。
无论开发桌面应用程序、Web 服务还是云原生解决方案,IronDrawing 都能确保您的 BarCode 处理代码在所有平台上保持可移植性和高效性,是现代 .NET 开发的理想选择。
常见问题解答
如何在非 Windows 平台上从 System.Drawing 对象读取 BarCode?
IronBarcode 通过 IronDrawing 自动处理来自 System.Drawing 对象的跨平台条形码读取,并将其转换为 AnyBitmap 格式。这就解决了 Microsoft System.Drawing 仅限于 Windows 的限制,使您可以在 MacOS 和 Linux 系统上无缝读取条形码。
什么是 IronDrawing,为什么它包含在条形码读取中?
IronDrawing 是 Iron Software 创建的免费开源库,为图形操作提供跨平台支持。从 NuGet 安装 IronBarcode 时会自动包含该库,通过将 System.Drawing 对象转换为兼容的 AnyBitmap 格式,可以在所有操作系统上从 System.Drawing 对象读取条形码。
如何转换 System.Drawing.Bitmap 以从中读取条形码?
您可以通过简单的投向 AnyBitmap 从 System.Drawing.Bitmap 读取条形码:`var results = BarcodeReader.Read((AnyBitmap)(new System.Drawing.Bitmap("yourImage.png"));`.IronBarcode 通过 IronDrawing 的隐式转换功能自动处理转换。
能否在 Linux 和 MacOS 上使用 System.Drawing 阅读 BarCode?
是的,IronBarcode 可通过 IronDrawing 从 Linux 和 MacOS 上的 System.Drawing 对象读取条形码,IronDrawing 可自动将 System.Drawing 对象转换为跨平台的 AnyBitmap 格式。这克服了微软对 System.Drawing 支持的 Windows 限制。
哪些类型的 System.Drawing 对象可用于条形码读取?
IronBarcode 支持从各种 System.Drawing 对象(包括 System.Drawing.Bitmap 和其他图像类型)读取条形码。通过 IronDrawing 的隐式铸造功能,这些对象会自动转换为 AnyBitmap,从而实现跨平台条形码扫描功能。
是否有简单的单行解决方案来从 System.Drawing 读取 BarCode?
是的,IronBarcode 提供了单行解决方案:`var results = BarcodeReader.Read((AnyBitmap)(new System.Drawing.Bitmap("yourImage.png"));`.这一行创建了一个 System.Drawing.Bitmap,通过 IronDrawing 将其转换为 AnyBitmap,并读取图像中存在的所有条码。
IronBarcode是否提供支持自定义条形码外观的功能?
是的,IronBarcode为条形码外观提供广泛的自定义选项,包括颜色、大小和文本注释,允许运您将条形码设计成符合您特定的设计要求。
IronBarcode如何帮助提高业务流程的效率?
IronBarcode通过快速准确的条码生成和读取提高了业务流程效率,减少了手动数据输入错误,并改善了库存和资产跟踪。
在项目中实现IronBarcode需要哪些编程技能?
了解C#编程的基础知识就足以在项目中实现IronBarcode,因为它提供了简单的方法和全面的文档来指导开发人员。
IronBarcode适合小项目和大型企业应用吗?
IronBarcode被设计为可扩展且多功能,适合需要强大条码解决方案的小项目和大型企业应用。

