如何從 System.Drawing 對象讀取條碼

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

海里海西米·賓·奧馬

System.Drawing 物件在 .NET 中被開發者廣泛使用於圖像處理相關任務。然而,微軟已經 停止支援System.Drawing 在其他作業系統如 MacOSLinux 上,IronBarcode 現在已經僅支援 Windows。這一重大變化給在非 Windows 作業系統上使用 IronBarcode 的開發者帶來了多重問題。這是因為處理條碼時通常會涉及使用到 圖形圖像字型 等物件。

為了解決這個問題,我們引入了一個替代方案: 鐵繪圖. 此免費開源的庫由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.DrawingIronSoftware.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)
VB   C#

從上面的程式碼片段中,我們加載了兩個條碼圖像作為System.Drawing.BitmapSystem.Drawing.Image。然後,我們通過將它們分配給AnyBitmap對象,將它們隱式轉換為AnyBitmap。接著,我們將這些對象添加到AnyBitmap列表中。

讀取 AnyBitmap 中的條碼

IronBarcode 可以輕鬆地在其所有方法中接受 IronSoftware.Drawing.AnyBitmap 對象,無需任何額外配置。這為使用 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 的功能區不僅限於圖像投射。它還廣泛用於其他圖像處理方面,例如在樣式化條碼和 QR 碼時有用的 顏色字體。用戶可以探索我們如何利用 IronDrawing 來 自訂和添加標誌到QR碼.

海里海西米·賓·奧馬

軟體工程師

和所有優秀的工程師一樣,Hairil 是一位熱衷學習的人。他正在精進自己對 C#、Python 和 Java 的知識,利用這些知識為 Iron Software 團隊的成員創造價值。Hairil 從馬來西亞的馬來西亞工藝大學加入了 Iron Software 團隊,他在那裡獲得了化學和過程工程學士學位。