How to Read Barcodes From System Drawing Objects

如何在 C# 中从 System.Drawing 读取条形码

This article was translated from English: Does it need improvement?
Translated
View the article in English

System.Drawing 对象在 .NET 中被开发人员广泛用于与图像处理相关的任务。 然而,微软已停止对其他操作系统(如MacOSLinux)上的System.Drawing 提供支持,现在仅支持Windows 。 这一重大变化给在 Windows 以外的操作系统上使用 IronBarcode 的开发人员带来了诸多问题。 这是因为处理条形码通常涉及使用图形图像字体等对象。

为了解决这个问题,我们引入了一种替代方案: IronDrawing 。 这个由 Iron Software 发起的免费**开源**库旨在简化在 Windows 以外的操作系统上运行该程序的过程。 这为我们的用户提供了友好的用户体验。 从 NuGet 安装 IronBarcode 后,IronDrawing 将自动包含在您的项目中。

快速入门:使用 AnyBitmap 一行轻松读取条形码

这段代码片段展示了 IronBarcode 如何轻松读取条形码:它创建了一个 System.Drawing.Bitmap 对象,并让 IronDrawing 将其隐式转换为 AnyBitmap 类型。只需一行代码,任何操作系统上的开发者都能快速轻松地获得结果。

Nuget Icon立即开始使用 NuGet 创建 PDF 文件:

  1. 使用 NuGet 包管理器安装 IronBarcode

    PM > Install-Package BarCode

  2. 复制并运行这段代码。

    var results = IronBarCode.BarcodeReader.Read((AnyBitmap)(new System.Drawing.Bitmap("yourImage.png")));
  3. 部署到您的生产环境中进行测试

    立即开始在您的项目中使用 IronBarcode,免费试用!
    arrow pointer

系统转换。绘制到 AnyBitmap

System.Drawing读取条形码只需将对象强制转换为 AnyBitmap 即可。IronDrawing 的设计理念就是易于使用。 因此,IronDrawing 支持将System.Drawing中的图像对象隐式转换为Iron Software.Drawing中的图像对象,称为AnyBitmap

除了System.Drawing对象之外,我们也支持从其他类型的对象进行类型转换,包括:

  • System.Drawing.Bitmap
  • System.Drawing.Image
  • SkiaSharp.SKBitmap
  • SkiaSharp.SKImage
  • SixLabors.ImageSharp

用户可以参考以下代码示例对上述对象进行类型转换。 下面的代码片段演示了如何将System.Drawing objects中的条形码图像转换为Iron Software.Drawing.AnyBitmap 。 这是一个简单的例子:

: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)
$vbLabelText   $csharpLabel

从上面的代码片段中,我们加载了两个条形码图像,分别是System.Drawing.BitmapSystem.Drawing.Image 。 然后,我们只需将它们赋值给AnyBitmap对象,即可隐式地将它们转换为 AnyBitmap 类型。 随后,我们将这些对象添加到AnyBitmap列表中。

从 AnyBitmap 读取条形码

IronBarcode 的所有方法都可以直接接受Iron Software.Drawing.AnyBitmap对象,无需任何额外的配置。 这为使用 IronBarcode 和System.Drawing对象(Windows 以外的操作系统不支持这些对象)的开发人员提供了便利。 下面的代码片段演示了如何实现这一点。

: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;

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 barcode
$vbLabelText   $csharpLabel

上面的代码片段是对前一个代码片段的扩展。 一旦我们填充了 AnyBitmap 列表,我们就遍历该列表,并将每个 AnyBitmap 对象的Read方法作为参数调用,然后返回IronBarcode.BarcodeResults 。 然后我们遍历返回的对象,将条形码值打印到控制台。

Iron Software.Drawing的功能范围不仅限于铸造图像。 它还被大量用于图像处理的其他方面,例如用于条形码和二维码样式设计的颜色字体。 用户可以探索我们如何使用 IronDrawing 来定制二维码并添加徽标

常见问题解答

我如何从.NET C#中的System.Drawing对象读取条码?

您可以通过结合使用IronBarcode和IronDrawing,从System.Drawing对象中读取条码。首先,使用IronDrawing将您的System.Drawing对象转换为AnyBitmap,然后使用IronBarcode的Read方法读取条码。

什么是IronDrawing及其在条码读取中的作用?

IronDrawing是铁软件公司提供的一个免费开源库,允许将System.Drawing对象隐式转换为AnyBitmap。这使这些对象可以与IronBarcode兼容,从而在非Windows操作系统上进行条码读取。

我可以使用IronBarcode在MacOS和Linux上读取条码吗?

可以,通过使用IronDrawing,可以将System.Drawing对象转换为AnyBitmap,从而使IronBarcode可以在MacOS和Linux上读取条码,克服System.Drawing仅限于Windows的限制。

哪些图像对象类型可以转换为AnyBitmap以进行条码读取?

除了System.Drawing对象,您可以将System.Drawing.Bitmap、System.Drawing.Image、SkiaSharp.SKBitmap、SkiaSharp.SKImage和SixLabors.ImageSharp对象转换为AnyBitmap,以使用IronBarcode进行条码读取。

如何使用IronBarcode显示检测到的条码值?

在使用IronBarcode的Read方法读取条码后,通过BarcodeResult数组进行迭代,并将每个条码值打印到控制台。

从NuGet安装条码读取库时会包含IronDrawing吗?

会,当您从NuGet中安装IronBarcode时,IronDrawing会自动包含在您的项目中,为条码读取提供无缝集成。

图像对象的隐式转换如何帮助条码处理?

使用IronDrawing将图像对象隐式转换为AnyBitmap简化了使System.Drawing对象与IronBarcode兼容的过程,增强了在各种操作系统上进行条码处理的能力。

Hairil Hasyimi Bin Omar
软件工程师
如所有伟大的工程师一般,Hairil 是个热心的学习者。他正在提高对 C#、Python 和 Java 的知识,并利用这些知识为 Iron Software 团队成员增值。Hairil 从马来西亚的玛拉工业大学加入 Iron Software 团队,获得化学与工艺工程学士学位。
准备开始了吗?
Nuget 下载 1,979,979 | Version: 2025.11 刚刚发布