如何从 System.Drawing 对象读取条形码

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

海瑞尔 哈西米 本 奥马尔

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

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

适用于的C# NuGet库

安装使用 NuGet

Install-Package BarCode
Java PDF JAR

下载 DLL

下载DLL

手动安装到你的项目中

适用于的C# NuGet库

安装使用 NuGet

Install-Package BarCode
Java PDF JAR

下载 DLL

下载DLL

手动安装到你的项目中

开始在您的项目中使用IronPDF,并立即获取免费试用。

第一步:
green arrow pointer

查看 IronBarcodeNuget 用于快速安装和部署。它有超过800万次下载,正在使用C#改变。

适用于的C# NuGet库 nuget.org/packages/BarCode/
Install-Package BarCode

考虑安装 IronBarcode DLL 直接。下载并手动安装到您的项目或GAC表单中: IronBarCode.zip

手动安装到你的项目中

下载DLL

将 System.Drawing 转换为 AnyBitmap

从 System.Drawing 读取条形码只需将对象转换为 AnyBitmap。IronDrawing 在设计之初就考虑到了易用性。因此,IronDrawing 支持将System.Drawing中的图像对象隐式转换为IronSoftware.Drawing中称为AnyBitmap的图像对象。

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

  • 系统.绘图.位图
  • 系统.绘图.图像
  • 系统.绘图.位图
  • 系统.绘图.图像

  • 六实验室.ImageSharp SkiaSharp.SKImage**

用户可参考以下内容 代码示例 用于上述对象铸造。下面的代码片段演示了如何将条形码图像从System.Drawing 对象转换为IronSoftware.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)
VB   C#

在上面的代码片段中,我们以 System.Drawing.BitmapSystem.Drawing.Image 的形式加载了两个条形码图像。然后,我们通过将它们赋值给 AnyBitmap 对象,将它们隐式地转换为 AnyBitmap。随后,我们将这些对象添加到 AnyBitmap 列表中。

从 AnyBitmap 读取条形码

IronBarcode在其所有方法中均可接受IronSoftware.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
VB   C#

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

IronSoftware.Drawing中的功能区域不仅限于铸造图像。它还被大量用于其他图像处理方面,如颜色字体**,这在条形码和二维码的样式设计中非常有用。用户可以探索我们如何利用 IronDrawing 来 自定义 QR 码并将徽标添加到 QR 码中.

海瑞尔 哈西米 本 奥马尔

软件工程师

像所有优秀的工程师一样,Hairil 是一个热衷学习的人。他正在精进自己的 C#、Python 和 Java 知识,并利用这些知识为 Iron Software 团队成员增添价值。Hairil 毕业于马来西亚的马来西亚工艺大学(Universiti Teknologi MARA),获得了化学与工艺工程学士学位,然后加入了 Iron Software 团队。