如何從 System.Drawing 對象讀取條碼
System.Drawing 物件在 .NET 中被開發者廣泛用於與圖像處理相關的任務。 然而,微軟已經停止支援 System.Drawing在其他操作系統上,如MacOS和Linux,現在僅支持Windows。 這一重大變化對於在Windows以外的作業系統上使用IronBarcode的開發者們造成了多個問題。 這是因為處理條碼通常涉及使用像圖形、圖片和字體之類的對象。
為了解決這個問題,我們引入了一個替代方案:IronDrawing。 這個免費且開源的庫,由 IronSoftware 發起,旨在簡化使其在非 Windows 作業系統上運作的過程。 這為我們的用戶提供了友好的使用體驗。 一旦您從 NuGet 安裝 IronBarcode,IronDrawing 將自動包含在您的項目中。
開始使用 IronBarcode
立即在您的專案中使用IronBarcode,並享受免費試用。
如何從 System.Drawing 對象讀取條碼
將 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 objects的條碼影像轉換為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 且搭配不支援 Windows 以外操作系統的 System.Drawing 物件的開發人員提供了易用性。 以下程式碼片段示範了如何做到這一點。
: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碼。