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

海瑞尔 哈西米 本 奥马尔
海瑞尔 哈西米 本 奥马尔
2023年九月13日
更新 2024年十二月10日
分享:
This article was translated from English: Does it need improvement?
Translated
View the article in English

System.Drawing 对象在 .NET 中被开发者广泛用于相关图像处理的任务。 然而,微软已停止对其他操作系统上的System.Drawing的支持,如MacOSLinux,现在仅支持Windows。 这一重大变化给使用 IronBarcode 的开发者在非 Windows 操作系统上造成了多个问题。 这是因为处理条形码通常涉及使用诸如图形图像字体之类的对象。

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

开始使用 IronBarcode

立即在您的项目中开始使用IronBarcode,并享受免费试用。

第一步:
green arrow pointer


将 System.Drawing 转换为 AnyBitmap

从 System.Drawing 读取条形码只需将对象转换为 AnyBitmap。IronDrawing 的设计初衷是易用性。 因此,IronDrawing 支持将图像对象从 System.Drawing 隐式转换为 IronSoftware.Drawing 图像对象,称为 AnyBitmap

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

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

    用户可以参考以下代码示例以转换上述对象。 以下是一个代码片段,演示了如何将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)
$vbLabelText   $csharpLabel

从上述代码片段中,我们将两个条形码图像加载为System.Drawing.BitmapSystem.Drawing.Image。 我们通过将它们赋值给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
$vbLabelText   $csharpLabel

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

IronSoftware.Drawing 的功能区域不仅限于图像转换。 它也广泛应用于其他图像处理方面,例如对条形码和二维码样式有用的颜色字体。 用户可以探索我们如何使用IronDrawing 自定义和添加徽标到QR码

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