How to Read Barcodes From System.Drawing in C
IronBarcode 能透過 IronDrawing 將 System.Drawing 物件自動轉換為 AnyBitmap,從而實現所有作業系統上的 BARCODE 讀取功能,解決了 Microsoft 僅限 Windows 系統支援 System.Drawing 的限制。
簡介
System.Drawing 物件在 .NET 中廣泛用於影像處理任務。 然而,微軟已停止在 macOS 和 Linux 平台上支援 System.Drawing,目前僅支援 Windows 系統。 此變更為在非 Windows 作業系統上使用 IronBarcode 的開發人員帶來了問題,因為處理條碼通常涉及圖形、影像和字型。
為解決此問題,我們推出了 IronDrawing。 此由 Iron Software 開發的免費開源函式庫,簡化了跨平台支援,並提供無縫的使用體驗。 當您從 NuGet 安裝 IronBarcode 時,IronDrawing 會自動包含在您的專案中。
對於初次接觸BarCode讀取的開發者,請參閱我們的《BarCode讀取綜合教學》,內容涵蓋基礎概念與基本使用模式。 若您需要處理各種圖像格式,我們關於從圖像讀取BARCODE的指南提供了更多背景資訊與範例。
快速入門:僅需一行簡單程式碼,即可使用 AnyBitmap 讀取 BARCODE
此程式碼片段展示了 IronBarcode 如何透過建立 System.Drawing.Bitmap,並讓 IronDrawing 將其隱式轉換為 AnyBitmap 來讀取 BARCODE。 只需一行代碼,無論使用何種作業系統,開發者都能快速獲得結果。
簡化工作流程(5 個步驟)
- 從以下位置下載用於讀取 BARCODE 的 C# 函式庫:
System.Drawing - 請使用
IronDrawing來將System.Drawing物件轉換為AnyBitmap - 請使用
Read方法從BarCodeAnyBitmap物件 - 將偵測到的BarCode值顯示於控制台
- 閱讀另一篇文章,了解如何
IronDrawing如何用於處理顏色與字型
如何將 System.Drawing 物件轉換為 AnyBitmap?
要讀取 System.Drawing 格式的 BARCODE,需將物件轉換為 AnyBitmap 格式。 IronDrawing 專為易用性而設計,並支援將 System.Drawing 中的影像物件隱式轉換為 IronSoftware.Drawing 中的 AnyBitmap 影像物件。
除了 System.Drawing 物件之外,我們也支援從其他類型進行型別轉換:
System.Drawing.BitmapSystem.Drawing.ImageSkiaSharp.SKBitmapSkiaSharp.SKImageSixLabors.ImageSharp
請參閱此程式碼範例,了解如何對上述物件進行型別轉換。 以下示範如何將 System.Drawing 物件中的 BARCODE 影像轉換為 IronSoftware.Drawing.AnyBitmap:
哪些 System.Drawing 類型可以進行型別轉換?
: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 物件與 IronBarcode 透過 IronDrawing 進行無縫整合。 此相容性涵蓋多種 BARCODE 格式,詳見我們的"支援 BARCODE 格式指南",包括 QR 碼、Code 128、Code 39 以及許多其他格式。
為什麼隱式轉換能生效?
在上述程式碼中,我們載入了兩張BARCODE圖片,分別為 System.Drawing.Bitmap 和 System.Drawing.Image。 接著,我們透過將它們賦值給 AnyBitmap 物件,將其隱式轉換為 AnyBitmap,然後將這些物件加入 AnyBitmap 清單中。
IronDrawing 的隱式轉換機制採用運算子重載,可在 System.Drawing 類型與 AnyBitmap 之間提供透明的轉換。 此設計模式讓開發人員能在維持現有程式碼的同時,獲得跨平台相容性。 轉換過程會保留所有影像屬性,包括解析度、色彩深度及像素資料,確保無任何畫質損失。
何時該使用顯式轉換與隱式轉換?
雖然隱式轉換帶來便利,但在某些情境下,顯式轉換可能更為合適:
// Implicit casting - clean and simple for straightforward conversions
System.Drawing.Bitmap systemBitmap = new System.Drawing.Bitmap("barcode.png");
AnyBitmap anyBitmap = systemBitmap; // Implicit cast
// Explicit casting - useful when type clarity is important
System.Drawing.Image systemImage = System.Drawing.Image.FromFile("qrcode.jpg");
AnyBitmap explicitBitmap = (AnyBitmap)systemImage; // Explicit cast
// When working with nullable types or conditional logic
System.Drawing.Bitmap? nullableBitmap = GetBitmapFromSource();
if (nullableBitmap != null)
{
AnyBitmap result = (AnyBitmap)nullableBitmap; // Explicit cast for clarity
// Process the barcode
}
// Implicit casting - clean and simple for straightforward conversions
System.Drawing.Bitmap systemBitmap = new System.Drawing.Bitmap("barcode.png");
AnyBitmap anyBitmap = systemBitmap; // Implicit cast
// Explicit casting - useful when type clarity is important
System.Drawing.Image systemImage = System.Drawing.Image.FromFile("qrcode.jpg");
AnyBitmap explicitBitmap = (AnyBitmap)systemImage; // Explicit cast
// When working with nullable types or conditional logic
System.Drawing.Bitmap? nullableBitmap = GetBitmapFromSource();
if (nullableBitmap != null)
{
AnyBitmap result = (AnyBitmap)nullableBitmap; // Explicit cast for clarity
// Process the barcode
}
Imports System.Drawing
' Implicit casting - clean and simple for straightforward conversions
Dim systemBitmap As New Bitmap("barcode.png")
Dim anyBitmap As AnyBitmap = systemBitmap ' Implicit cast
' Explicit casting - useful when type clarity is important
Dim systemImage As Image = Image.FromFile("qrcode.jpg")
Dim explicitBitmap As AnyBitmap = CType(systemImage, AnyBitmap) ' Explicit cast
' When working with nullable types or conditional logic
Dim nullableBitmap As Bitmap = GetBitmapFromSource()
If nullableBitmap IsNot Nothing Then
Dim result As AnyBitmap = CType(nullableBitmap, AnyBitmap) ' Explicit cast for clarity
' Process the barcode
End If
常見的型別轉換錯誤有哪些?
在將 System.Drawing 轉換為 AnyBitmap 時,開發人員可能會遇到:
- 空參考例外:在進行型別轉換前,請先確認您的
System.Drawing物件並非 null - 未支援格式例外情況:某些特殊圖像格式需要預先轉換
- 記憶體問題:大型圖片需要適當的釋放機制
針對型別轉換問題的疑難排解,我們的疑難排解指南提供了BarCode辨識過程中常見問題的解決方案。
如何從 AnyBitmap 物件中讀取 BarCode?
IronBarcode 在所有方法中皆可接受 IronSoftware.Drawing.AnyBitmap 物件,無需額外設定。 這能簡化在非 Windows 作業系統上使用 System.Drawing 物件時的開發流程。 以下程式碼示範了這一點:
哪些方法接受 AnyBitmap 參數?
: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;
// Create a list of image file paths to read barcodes from
List<string> barcodeFiles = new List<string>
{
"test1.jpg",
"test2.png"
};
foreach (var barcodeFile in barcodeFiles)
{
// Read the barcode from file path
var results = BarcodeReader.Read(barcodeFile);
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
' Create a list of image file paths to read barcodes from
Dim barcodeFiles As New List(Of String) From {
"test1.jpg",
"test2.png"
}
For Each barcodeFile In barcodeFiles
' Read the barcode from file path
Dim results = BarcodeReader.Read(barcodeFile)
For Each result In results
' Output the detected barcode value
Console.WriteLine(result.Value)
Next
Next
除了基本的 Read 方法外,IronBarcode 還提供數種可接受 AnyBitmap 參數的方法。 關於進階應用情境,請參閱我們關於讀取多個BarCode的指南,該指南展示了如何高效處理單一影像中的多個BarCode:
// Advanced barcode reading with options
var readerOptions = new BarcodeReaderOptions
{
// Specify barcode types to search for
ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Co/de128,
// Enable machine learning for better accuracy
UseML = true,
// Set confidence threshold
Confidence = 0.95
};
// Read with specific options
var advancedResults = BarcodeReader.Read(anyBitmap, readerOptions);
// Advanced barcode reading with options
var readerOptions = new BarcodeReaderOptions
{
// Specify barcode types to search for
ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Co/de128,
// Enable machine learning for better accuracy
UseML = true,
// Set confidence threshold
Confidence = 0.95
};
// Read with specific options
var advancedResults = BarcodeReader.Read(anyBitmap, readerOptions);
' Advanced barcode reading with options
Dim readerOptions As New BarcodeReaderOptions With {
' Specify barcode types to search for
.ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128,
' Enable machine learning for better accuracy
.UseML = True,
' Set confidence threshold
.Confidence = 0.95
}
' Read with specific options
Dim advancedResults = BarcodeReader.Read(anyBitmap, readerOptions)
如何處理多個BarCode結果?
上述程式碼延續了前一個範例。 在填入 AnyBitmap 清單後,我們遍歷該清單,並對每個 AnyBitmap 物件呼叫 Read 方法,該方法返回 IronBarCode.BarcodeResults。 接著,我們遍歷結果,將BarCode值輸出至控制台。
處理多個BarCode時,請利用平行處理以提升效能:
// Parallel processing for multiple barcode images
var barcodeFiles = Directory.GetFiles("barcodes/", "*.png");
var allResults = new ConcurrentBag<BarcodeResult>();
Parallel.ForEach(barcodeFiles, file =>
{
var bitmap = new System.Drawing.Bitmap(file);
var anyBitmap = (AnyBitmap)bitmap;
var results = BarcodeReader.Read(anyBitmap);
foreach (var result in results)
{
allResults.Add(result);
}
bitmap.Dispose(); // Clean up resources
});
// Process all results
foreach (var result in allResults)
{
Console.WriteLine($"Found {result.BarcodeType}: {result.Value}");
}
// Parallel processing for multiple barcode images
var barcodeFiles = Directory.GetFiles("barcodes/", "*.png");
var allResults = new ConcurrentBag<BarcodeResult>();
Parallel.ForEach(barcodeFiles, file =>
{
var bitmap = new System.Drawing.Bitmap(file);
var anyBitmap = (AnyBitmap)bitmap;
var results = BarcodeReader.Read(anyBitmap);
foreach (var result in results)
{
allResults.Add(result);
}
bitmap.Dispose(); // Clean up resources
});
// Process all results
foreach (var result in allResults)
{
Console.WriteLine($"Found {result.BarcodeType}: {result.Value}");
}
Imports System.IO
Imports System.Collections.Concurrent
Imports System.Drawing
Imports System.Threading.Tasks
' Parallel processing for multiple barcode images
Dim barcodeFiles = Directory.GetFiles("barcodes/", "*.png")
Dim allResults = New ConcurrentBag(Of BarcodeResult)()
Parallel.ForEach(barcodeFiles, Sub(file)
Dim bitmap = New Bitmap(file)
Dim anyBitmap = CType(bitmap, AnyBitmap)
Dim results = BarcodeReader.Read(anyBitmap)
For Each result In results
allResults.Add(result)
Next
bitmap.Dispose() ' Clean up resources
End Sub)
' Process all results
For Each result In allResults
Console.WriteLine($"Found {result.BarcodeType}: {result.Value}")
Next
我還能使用哪些 IronDrawing 功能?
IronSoftware.Drawing 的功能不僅限於影像轉換。 它處理諸如顏色和字型等影像處理方面,這些對於設計 BARCODE 和 QR 碼的樣式非常有用。 探索我們如何利用 IronDrawing 來自訂並在 QR 碼中加入標誌。
IronDrawing 提供強大的影像處理功能,可與 BarCode 處理功能相輔相成:
// Using IronDrawing for image preprocessing
using IronSoftware.Drawing;
// Load and preprocess an image before barcode reading
AnyBitmap preprocessedImage = AnyBitmap.FromFile("noisy-barcode.jpg");
// Apply image filters to improve barcode readability
preprocessedImage = preprocessedImage.ToGrayScale();
preprocessedImage = preprocessedImage.Co/ntrast(1.5); // Increase contrast
preprocessedImage = preprocessedImage.Sharpen(); // Sharpen image
// Read the preprocessed barcode
var improvedResults = BarcodeReader.Read(preprocessedImage);
// Using IronDrawing for image preprocessing
using IronSoftware.Drawing;
// Load and preprocess an image before barcode reading
AnyBitmap preprocessedImage = AnyBitmap.FromFile("noisy-barcode.jpg");
// Apply image filters to improve barcode readability
preprocessedImage = preprocessedImage.ToGrayScale();
preprocessedImage = preprocessedImage.Co/ntrast(1.5); // Increase contrast
preprocessedImage = preprocessedImage.Sharpen(); // Sharpen image
// Read the preprocessed barcode
var improvedResults = BarcodeReader.Read(preprocessedImage);
Imports IronSoftware.Drawing
' Load and preprocess an image before barcode reading
Dim preprocessedImage As AnyBitmap = AnyBitmap.FromFile("noisy-barcode.jpg")
' Apply image filters to improve barcode readability
preprocessedImage = preprocessedImage.ToGrayScale()
preprocessedImage = preprocessedImage.Contrast(1.5) ' Increase contrast
preprocessedImage = preprocessedImage.Sharpen() ' Sharpen image
' Read the preprocessed barcode
Dim improvedResults = BarcodeReader.Read(preprocessedImage)
若遇到需要進行特定影像修正的情境,請參閱我們的影像修正指南,其中詳述了如何運用濾鏡來提升BARCODE的可讀性。
為何選擇 IronDrawing 而非 System.Drawing?
IronDrawing 相較於 System.Drawing 具備以下顯著優勢:
- 跨平台支援:與
System.Drawing(僅限 .NET Core/5+ 版本的 Windows 系統)不同,本工具可在 Windows、Linux 和 macOS 上無縫運作 - 現代化架構:基於
SkiaSharp和ImageSharp打造,以提升效能與記憶體管理 - 簡化 API:在保留類似
System.Drawing的熟悉介面的同時,增添現代化的便利功能 - 積極開發中:定期更新與改進,有別於
System.Drawing處於維護模式 - 更佳的整合性:專為與 Iron Software產品搭配使用而設計,以實現最佳效能
關於部署考量,特別是雲端環境,請參閱我們關於部署至 Azure 及部署至 AWS 的指南,其中包含有關使用 IronDrawing 實現跨平台相容性的具體說明。
無論是開發桌面應用程式、網路服務或雲原生解決方案,IronDrawing 都能確保您的 BARCODE 處理程式碼在所有平台上保持可移植性與高效能,使其成為現代 .NET 開發的理想選擇。
常見問題
如何在非 Windows 平臺上從 System.Drawing 物件讀取條碼?
IronBarcode 自動處理從 System.Drawing 物件跨平台的條碼讀取,通過 IronDrawing 轉換為 AnyBitmap 格式。這解決了 Microsoft 的 System.Drawing 僅限於 Windows 的限制,讓您可以在 MacOS 和 Linux 系統上無縫讀取條碼。
什麼是 IronDrawing,為什麼它包含在條碼讀取中?
IronDrawing 是 Iron Software 創建的一個免費、開源的程式庫,提供跨平台的圖形操作支援。當您從 NuGet 安裝 IronBarcode 時,它會自動包含在內,並透過將 System.Drawing 物件轉換為相容的 AnyBitmap 格式來啟用所有作業系統上的條碼讀取。
如何轉換 System.Drawing.Bitmap 以從中讀取條碼?
您可以使用簡單地轉換為 AnyBitmap 來從 System.Drawing.Bitmap 讀取條碼:`var results = BarcodeReader.Read((AnyBitmap)(new System.Drawing.Bitmap("yourImage.png")));`。IronBarcode 透過 IronDrawing 的隱式轉換功能自動處理轉換。
我可以在 Linux 和 MacOS 上使用 System.Drawing 讀取條碼嗎?
可以的,IronBarcode 通過 IronDrawing 啟用 Linux 和 MacOS 上的 System.Drawing 物件條碼讀取,自動將 System.Drawing 物件轉換為跨平台的 AnyBitmap 格式。這克服了 Microsoft 對 System.Drawing 支援僅限於 Windows 的限制。
哪些類型的 System.Drawing 物件可以用於條碼讀取?
IronBarcode 支援從各種 System.Drawing 物件(包括 System.Drawing.Bitmap 和其他圖像類型)中讀取條碼。這些將自動透過 IronDrawing 的隱式轉換功能轉換為 AnyBitmap,實現跨平台的條碼掃描功能。
是否有從 System.Drawing 讀取條碼的簡單單行解決方案?
是的,IronBarcode 提供單行解決方案:`var results = BarcodeReader.Read((AnyBitmap)(new System.Drawing.Bitmap("yourImage.png")));`。這行程式碼創建了一個 System.Drawing.Bitmap,通過 IronDrawing 將其轉換為 AnyBitmap,並讀取圖像中存在的所有條碼。
IronBarcode是否提供自定義條碼外觀的支持?
是的,IronBarcode提供了廣泛的條碼外觀自定義選項,包括顏色、大小和文字註釋,讓您可以根據具體設計需求定制條碼。
IronBarcode如何幫助改善業務流程效率?
IronBarcode通過使條碼生成和讀取快速且準確來提高業務流程效率,減少手動數據輸入錯誤,並改善庫存和資產追蹤。
將IronBarcode實現於專案中需要什麼程式設計技能?
基本的C#程式設計知識足以將IronBarcode實現於專案中,因為它提供了簡單的方法和全面的文檔來指導開發者。
IronBarcode適合於小型專案和大型企業應用嗎?
IronBarcode設計為可擴展且多功能,使其適合小型專案和需要強大條碼解決方案的大型企業應用。

