如何從系統繪圖物件讀取條碼

How to Read Barcodes From System.Drawing in C

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

IronBarcode能夠透過自動將 System.Drawing 物件轉換為 AnyBitmapIronDrawing,在所有作業系統上讀取條碼,從而解決了 Microsoft 對 System.Drawing 支援的 Windows 限制。

介紹

System.Drawing 物件在.NET中廣泛用於影像處理任務。 然而,微軟已停止對MacOSLinux上的System.Drawing 的支持,現在僅支援Windows 。 這項變更為在非 Windows 作業系統上使用IronBarcode的開發人員帶來了問題,因為處理條碼通常涉及圖形映像字體

為了解決這個問題,我們引入了IronDrawing 。 這個由 IronSoftware 創建的免費**開源**庫簡化了跨平台支持,並提供了無縫體驗。 當您從NuGet安裝IronBarcode時,IronDrawing 會自動包含在您的專案中。

對於剛接觸條碼讀取的開發人員,請參閱我們全面的條碼讀取教程,其中涵蓋了基本概念和基本使用模式。 如果您正在處理各種圖像格式,我們的圖像條碼讀取指南提供了更多背景資訊和範例。

快速入門:使用 CODE-848 一行輕鬆讀取條碼。

此程式碼片段展示了IronBarcode如何透過建立 System.Drawing.Bitmap 並讓 IronDrawing 將其隱式轉換為 AnyBitmap 來讀取條碼。 只需一行程式碼,任何作業系統上的開發者都能快速獲得結果。

  1. 使用NuGet套件管理器安裝https://www.nuget.org/packages/BarCode

    PM > Install-Package BarCode
  2. 複製並運行這段程式碼。

    var results = IronBarCode.BarcodeReader.Read((AnyBitmap)(new System.Drawing.Bitmap("yourImage.png")));
  3. 部署到您的生產環境進行測試

    今天就在您的專案中開始使用免費試用IronBarcode

    arrow pointer

如何將 System.Drawing 物件轉換為 AnyBitmap

System.Drawing 讀取條碼需要將物件強制轉換為 AnyBitmapIronDrawing 的設計旨在方便使用,並支援將 System.Drawing 中的圖像對象隱式轉換為 IronSoftware.Drawing 中的圖像對象,稱為 AnyBitmap

除了 System.Drawing 物件之外,我們也支援從其他類型進行類型轉換:

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

請參閱以下程式碼範例,以了解如何對上述物件進行類型轉換。 以下示範如何將 System.Drawing 物件中的條碼影像轉換為 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);
$vbLabelText   $csharpLabel

此程式碼示範了 System.Drawing 物件與 IronBarcode 透過 IronDrawing 之間的無縫整合。 此相容性涵蓋了各種條碼格式,詳情請參閱我們支援的條碼格式指南,包括二維碼、Code 128、Code 39 等。

為什麼隱式角色塑造有效?

在上面的程式碼中,我們載入了兩個條碼圖像,分別為 System.Drawing.BitmapSystem.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
}
$vbLabelText   $csharpLabel

常見的選角錯誤有哪些?

System.Drawing 轉換為 AnyBitmap 時,開發人員可能會遇到以下問題:

1.空引用異常:在進行型別轉換之前,請先驗證您的 System.Drawing 物件是否為空。 2.不支援的格式例外:某些特殊影像格式需要預先轉換 3.記憶體問題:大型影像需要適當的記憶體釋放模式

針對鑄造問題,我們的故障排除指南提供了條碼識別過程中常見問題的解決方案。

如何從 AnyBitmap 物件讀取條碼?

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);
    }
}
$vbLabelText   $csharpLabel

除了基本的 Read 方法之外, IronBarcode也提供了幾個接受 AnyBitmap 參數的方法。 對於更高級的應用場景,請參閱我們的多條碼讀取指南,其中演示瞭如何高效處理單張影像中的多個條碼:

// Advanced barcode reading with options
var readerOptions = new BarcodeReaderOptions
{
    // Specify barcode types to search for
    ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128,
    // 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.Code128,
    // Enable machine learning for better accuracy
    UseML = true,
    // Set confidence threshold
    Confidence = 0.95
};

// Read with specific options
var advancedResults = BarcodeReader.Read(anyBitmap, readerOptions);
$vbLabelText   $csharpLabel

如何處理多個條碼結果?

上面的程式碼是對前面範例的擴展。 在填充 AnyBitmap 列表後,我們遍歷該列表,並對每個 AnyBitmap 物件呼叫 Read 方法,該方法傳回IronBarCode.BarcodeResults 。 然後我們遍歷結果,將條碼值列印到控制台。

處理多個條碼時,利用並行處理可以提高效能:

// 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}");
}
$vbLabelText   $csharpLabel

我還能使用哪些其他 IronDrawing 功能?

IronSoftware.Drawing 功能不僅限於影像投射。 它處理圖像處理方面的問題,例如顏色字體,這對於條碼和二維碼的樣式設計非常有用。 探索我們如何使用 IronDrawing 來客製化二維碼並添加徽標

IronDrawing 提供強大的影像處理功能,是條碼處理的補充:

// 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.Contrast(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.Contrast(1.5); // Increase contrast
preprocessedImage = preprocessedImage.Sharpen(); // Sharpen image

// Read the preprocessed barcode
var improvedResults = BarcodeReader.Read(preprocessedImage);
$vbLabelText   $csharpLabel

對於需要進行特定影像校正的場景,我們的影像校正指南詳細介紹如何使用濾鏡來增強條碼的可讀性。

為什麼選擇 IronDrawing 而不是 System.Drawing

IronDrawingSystem.Drawing 有顯著優勢:

1.跨平台支援:與 System.Drawing(僅限 Windows 版.NET Core/5+)不同,可在 Windows、Linux 和 macOS 上無縫運作。 2.現代架構:基於 SkiaSharpImageSharp 構建,以實現更佳的性能和內存管理 3.簡化 API :保持熟悉的 System.Drawing-like 接口,同時增加現代化的便利功能 4.積極開發:定期更新和改進,與處於維護模式的 System.Drawing 不同 5.更佳的整合性:專為與Iron Software產品實現最佳效能而設計

對於部署注意事項,特別是雲端環境,請參閱我們部署到 Azure部署到 AWS 的指南,其中包含有關使用 IronDrawing 實現跨平台相容性的具體說明。

無論是建立桌面應用程式、Web 服務或雲端原生解決方案,IronDrawing 都能確保您的條碼處理程式碼在所有平台上保持可移植性和高效性,使其成為現代.NET開發的理想選擇。

常見問題解答

如何在非 Windows 平台上從 System.Drawing 物件讀取 BarCode?

IronBarcode 可透過 IronDrawing 自動處理 System.Drawing 物件的跨平台條碼讀取,並將其轉換為 AnyBitmap 格式。這解決了 Microsoft System.Drawing 僅限 Windows 的限制,讓您可以在 MacOS 和 Linux 系統上無縫讀取條碼。

什麼是 IronDrawing,為什麼它包含在 BarCode 閱讀中?

IronDrawing 是由 Iron Software 開發的免費開源程式庫,提供跨平台圖形操作支援。當您從 NuGet 安裝 IronBarcode 時,它會自動包含在內,並將 System.Drawing 物件轉換為相容的 AnyBitmap 格式,從而實現在所有作業系統上讀取條碼。

如何轉換 System.Drawing.Bitmap 以從中讀取 BarCode?

您可以從 System.Drawing.Bitmap 讀取條碼,只要簡單地轉換為 AnyBitmap 即可:`var results = BarcodeReader.Read((AnyBitmap)(new System.Drawing.Bitmap("yourImage.png"));`.IronBarcode 通過 IronDrawing 的隱式轉換功能自動處理轉換。

我可以在 Linux 和 MacOS 上使用 System.Drawing 讀取 BarCode 嗎?

是的,IronBarcode 可透過 IronDrawing 從 Linux 和 MacOS 上的 System.Drawing 物件讀取條碼,IronDrawing 可自動將 System.Drawing 物件轉換為跨平台的 AnyBitmap 格式。這克服了微軟對於 System.Drawing 支援僅限於 Windows 的限制。

哪些類型的 System.Drawing 物件可用於條碼讀取?

IronBarcode 支援從各種 System.Drawing 物件讀取條碼,包括 System.Drawing.Bitmap 及其他影像類型。透過 IronDrawing 的 implicit casting 功能,這些物件會自動轉換為 AnyBitmap,以實現跨平台的條碼掃描功能。

是否有簡單的單行解決方案,可從 System.Drawing 讀取 BarCode?

是的,IronBarcode 提供了單行解決方案:`var results = BarcodeReader.Read((AnyBitmap)(new System.Drawing.Bitmap("yourImage.png"));`.這一行建立一個 System.Drawing.Bitmap,透過 IronDrawing 將它轉換成 AnyBitmap,並讀取影像中存在的所有 BarCode。

Hairil Hasyimi Bin Omar
軟體工程師
和所有优秀的工程师一样,Hairil 是個努力学习者。他正在细化自己的 C# 、Python 和 Java 知识,将这些知识應用于 Iron Software 各個团队成员以增加价值。Hairil 自马来西亚 Universiti Teknologi MARA 加入 Iron Software 团队,并以化学与工艺工程学士学位毕业。
準備好開始了嗎?
Nuget 下載 2,121,847 | 版本: 2026.3 剛剛發布
Still Scrolling Icon

還在捲動嗎?

想要快速證明? PM > Install-Package BarCode
執行範例 看您的字串變成 BarCode。