如何从 System.Drawing 对象读取条形码
System.Drawing 对象在 .NET 中被开发者广泛用于相关图像处理的任务。 然而,微软已停止对其他操作系统上的System.Drawing的支持,如MacOS和Linux,现在仅支持Windows。 这一重大变化给使用 IronBarcode 的开发者在非 Windows 操作系统上造成了多个问题。 这是因为处理条形码通常涉及使用诸如图形、图像和字体之类的对象。
为了解决这个问题,我们引入了一种替代解决方案:IronDrawing。 这款由IronSoftware发起的免费和开源库,旨在简化在非Windows操作系统上使其工作的过程。 这为我们的用户提供了友好的使用体验。 安装 NuGet 上的 IronBarcode 后,IronDrawing 将自动包含在您的项目中。
开始使用 IronBarcode
立即在您的项目中开始使用IronBarcode,并享受免费试用。
如何从 System.Drawing 对象读取条形码
- Download the C# library for reading barcodes from System.Drawing
- 利用 IronDrawing 将 System.Drawing 对象转换为 AnyBitmap
- 使用
Read
方法从AnyBitmap对象读取条形码 - 在控制台上显示检测到的条码值
- 阅读另一篇文章,了解 IronDrawing 如何用于处理颜色和字体
将 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)
从上述代码片段中,我们将两个条形码图像加载为System.Drawing.Bitmap 和 System.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
上面的代码片段是前一个的扩展。 一旦我们填充了 AnyBitmap 列表,我们遍历该列表,并在每个 AnyBitmap 对象上调用 Read
方法作为参数,然后返回IronBarcode.BarcodeResults。 然后我们遍历返回的对象,将条形码值打印到控制台上。
IronSoftware.Drawing 的功能区域不仅限于图像转换。 它也广泛应用于其他图像处理方面,例如对条形码和二维码样式有用的颜色和字体。 用户可以探索我们如何使用IronDrawing 自定义和添加徽标到QR码。