How to Read Barcodes From System.Drawing in C
IronBarcode允许从System.Drawing支持限制。
简介
System.Drawing对象在.NET中广泛用于图像处理任务。 然而,微软已经停止对System.Drawing在MacOS和Linux上的支持,现在仅支持Windows。 这一变化给在非 Windows 操作系统上使用 IronBarcode 的开发人员带来了问题,因为处理条形码通常涉及 图形、图像 和 字体。
为了解决这个问题,我们引入了IronDrawing。 这款免费且开源的库由Iron Software创建,简化了跨平台支持并提供无缝体验。 当您从NuGet安装IronBarcode时,IronDrawing会自动包含在您的项目中。
对于刚刚接触条形码阅读的开发人员,请参阅我们全面的 阅读条形码教程,其中涵盖基本概念和基本使用模式。 如果您正在使用各种图像格式,我们的从图像中读取 BarCode 指南提供了更多的上下文和示例。
AnyBitmap读取条码仅需一步@@--AH2EG--@@
此代码片段展示了IronBarcode如何通过创建AnyBitmap。 只需一行,任何操作系统上的开发人员都能快速获得结果。
最小工作流程(5 个步骤)
- 从
System.Drawing下载用于读取条形码的 C# 库 - 利用
IronDrawing将System.Drawing对象铸造为AnyBitmap - 使用
Read方法从AnyBitmap对象中读取条形码 - 在控制台上显示检测到的条码值
- 探索另一篇文章,了解
IronDrawing如何用于处理颜色和字体
如何将AnyBitmap?
从AnyBitmap。 AnyBitmap。
除了System.Drawing对象,我们还支持从其他类型转换:
System.Drawing.BitmapSystem.Drawing.ImageSkiaSharp.SKBitmapSkiaSharp.SKImageSixLabors.ImageSharp
请参阅此代码示例来铸造上述对象。 下面演示了如何将条码图像从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)
此代码演示了通过IronBarcode之间的无缝集成。 这种兼容性适用于各种条形码格式,详见我们的支持的条形码格式指南,包括 QR 码、Code 128、Code 39 和许多其他条形码格式。
隐式铸造为何有效?
在上面的代码中,我们将两个条码图像加载为System.Drawing.Image。 然后我们通过将它们赋值给AnyBitmap列表中。
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
哪些是常见的铸造错误?
在将AnyBitmap时,开发人员可能会遇到:
- 空引用异常:在转换之前确保您的
System.Drawing对象不是空的 2.不支持的格式异常:某些特殊的图像格式需要预先转换。 3.内存问题:大图片需要适当的处理模式
对于铸造问题的故障排除,我们的故障排除指南提供了条形码识别过程中常见问题的解决方案。
如何从AnyBitmap对象读取条码?
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
除了基本的AnyBitmap参数。 有关高级应用场景,请参阅我们的读取多个条形码指南,该指南演示了对单个图像中多个条形码的高效处理:
:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-system-drawing-5.cs
// Advanced barcode reading with options
var readerOptions = new BarcodeReaderOptions
{
// Specify barcode types to search for
ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128,
// Set confidence threshold
ConfidenceThreshold = 0.95
};
// Read with specific options
var advancedResults = BarcodeReader.Read(anyBitmap, readerOptions);
Imports System
' Advanced barcode reading with options
Dim readerOptions As New BarcodeReaderOptions With {
' Specify barcode types to search for
.ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128,
' Set confidence threshold
.ConfidenceThreshold = 0.95
}
' Read with specific options
Dim advancedResults = BarcodeReader.Read(anyBitmap, readerOptions)
如何处理多个 BarCode 结果?
上面的代码扩展了前面的示例。 在填充IronBarCode.BarcodeResults。 然后,我们对结果进行迭代,将 BarCode 值打印到控制台。
处理多个 BarCode 时,利用并行处理提高性能:
:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-system-drawing-6.cs
// 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 As New ConcurrentBag(Of BarcodeResult)()
Parallel.ForEach(barcodeFiles, Sub(file)
Dim bitmap As New Bitmap(file)
Dim anyBitmap As 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功能不仅限于转换图像。 它可以处理图像处理方面的问题,如 颜色和 字体,这对条形码和二维码的样式设计非常有用。 探索我们如何利用IronDrawing来定制和为二维码添加徽标。
IronDrawing提供强大的图像处理功能,补充条码处理:
:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-system-drawing-7.cs
// 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);
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)
对于需要进行特定图像校正的场景,我们的图像校正指南详细介绍了如何使用过滤器来增强条形码的可读性。
为什么选择System.Drawing?
System.Drawing的引人注目的优势:
- 跨平台支持:与仅在.NET Core/5+上支持Windows的
System.Drawing不同,在Windows、Linux和macOS上无缝运行 - 现代架构:基于
ImageSharp构建,以提高性能和内存管理 - 简化的API:在维持熟悉的
System.Drawing类似界面的同时添加了现代便利功能 - 积极的开发:定期更新和改进,而不像
System.Drawing处于维护模式 5.更佳的集成性:专为与 Iron Software 产品实现最佳性能而设计
有关部署考虑事项,特别是对于云环境,请参阅我们关于部署到Azure和部署到AWS的指南,其中包括关于使用IronDrawing的跨平台兼容性的具体说明。
无论是构建桌面应用程序、Web服务还是云原生解决方案,IronDrawing确保您的条码处理代码在所有平台上保持可移植和高效,使其成为现代.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被设计为可扩展且多功能,适合需要强大条码解决方案的小项目和大型企业应用。

