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

如何在 C# 中從 System.Drawing 讀取條碼。

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

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

介紹

<! -- 引言實作示意圖 --> <!--說明:說明程式碼概念的圖表或截圖 -->

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

為了解決這個問題,我們引入了 IronDrawing。 這個 免費開放原始碼的函式庫由 IronSoftware 所製作,可簡化跨平台支援並提供無縫的體驗。 當您從 NuGet 安裝 IronBarcode 時,IronDrawing 會自動包含在您的專案中。

對於初學條碼讀取的開發人員,請參閱我們全面的 條碼讀取教程,其中涵蓋基本概念和基本使用模式。 如果您正在使用各種圖像格式,我們的 從圖像讀取 BarCode 指南提供了額外的上下文和範例。

Quickstart: Read a barcode using AnyBitmap in one easy line快速入門:使用 AnyBitmap 輕鬆讀取條碼。

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

Nuget Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 IronBarcode

    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` 讀取條碼需要將物件鑄造為 `AnyBitmap` 。 `IronDrawing`是為了易於使用而設計的,並支援將圖像物件從`System.Drawing`隱式轉換成`IronSoftware.Drawing`圖像物件,稱為`AnyBitmap`。 除了`System.Drawing`物件之外,我們也支援從其他類型進行轉換: - **`System.Drawing.Bitmap`**系統.繪圖.位圖 - **`System.Drawing.Image`**系統繪圖。 - **<編碼>SkiaSharp.SKBitmap** - **<編碼>SkiaSharp.SKImage** - **<編碼>SixLabors.ImageSharp** 請參閱此[程式碼範例](/open-source/csharp/drawing/examples/cast-to-anybitmap/),以套用上述物件。 以下示範將條碼影像從 `System.Drawing` 物件轉換成 `IronSoftware.Drawing.AnyBitmap` 物件:

哪些 `System.Drawing` 類型可以被轉換? ```csharp :path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-system-drawing-cast-to-anybitmap.cs ``` 此程式碼透過 `IronDrawing` 展示 `System.Drawing` 物件與 `IronBarcode` 的無縫整合。 此相容性延伸至各種條碼格式,詳細資訊請參閱我們的 [ 支援的條碼格式指南](https://ironsoftware.com/csharp/barcode/get-started/supported-barcode-formats/),包括 QR code、Code 128、Code 39 以及許多其他格式。

隱含式鑄造為何有效? 在上面的程式碼中,我們以 `System.Drawing.Bitmap` 和 `System.Drawing.Image` 的方式載入兩個條碼影像。 然後,我們透過將它們指定給 `AnyBitmap` 物件,隱式地將它們轉換成 `AnyBitmap` 物件,然後將這些物件加入 `AnyBitmap` 清單中。 `IronDrawing` 的隱式轉換機制使用運算子重載,提供 `System.Drawing` 類型與 `AnyBitmap` 之間的透明轉換。 此設計模式可讓開發人員在維護現有程式碼的同時,獲得跨平台的相容性。 轉換時會保留所有影像屬性,包括解析度、色彩深度和像素資料,以確保不會造成品質上的損失。

何時應該使用顯式鑄造與隱式鑄造? 雖然隱式轉換提供了便利性,但在某些情況下,顯式轉換可能會是首選: ```csharp // 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 } ```

哪些是常見的鑄造錯誤? 當將 `System.Drawing` 轉換為 `AnyBitmap` 時,開發人員可能會遇到以下問題: 1.**空引用異常**:在轉換之前確認您的 `System.Drawing` 物件不是 null 2.**不支援的格式例外**:某些特殊的影像格式需要預先轉換。 3.**記憶體問題**:大型圖片需要適當的處理模式 針對鑄造問題的疑難排解,我們的 [ 疑難排解指南](https://ironsoftware.com/csharp/barcode/troubleshooting/barcode-not-recognized/) 提供條碼辨識過程中常見問題的解決方案。

如何從 `AnyBitmap` 物件讀取 BarCode? <!--說明:說明程式碼概念的圖表或截圖 --> IronBarcode 在所有方法中接受 **`IronSoftware.Drawing.AnyBitmap`**物件,無須額外設定。 在非 Windows 作業系統上使用 `System.Drawing` 物件時,可簡化開發工作。 以下程式碼演示了這一點:

哪些方法接受 `AnyBitmap` 參數? ```csharp :path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-system-drawing-read-anybitmap.cs ``` 除了基本的 `Read` 方法之外,IronBarcode 還提供了多種接受 `AnyBitmap` 參數的方法。 如需進階情況,請參閱我們的 [ 讀取多個 BarCode](https://ironsoftware.com/csharp/barcode/how-to/read-multiple-barcodes/) 指南,展示如何有效率地處理單一影像中的多個 BarCode: ```csharp // 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); ```

如何處理多個 BarCode 結果? 上面的程式碼延伸了之前的範例。 在填充 `AnyBitmap` 清單之後,我們遍歷它,並在每個 `AnyBitmap` 物件上呼叫 `Read` 方法,此方法會傳回 **`IronBarcode.BarcodeResults`**。 然後,我們反覆檢視結果,將 BarCode 值列印到控制台。 在處理多個 BarCode 時,利用平行處理來獲得更好的效能: ```csharp // Parallel processing for multiple barcode images var barcodeFiles = Directory.GetFiles("barcodes/", "*.png"); var allResults = new ConcurrentBag(); 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}"); } ```

我還可以使用哪些 `IronDrawing` 功能? `IronSoftware.Drawing`的功能不僅限於鑄造圖像。 它可處理影像處理方面的問題,例如對條碼和 QR 代碼造型有用的 **色彩**和 **字體**。 探索我們如何利用 `IronDrawing` 來 [客製化及新增標誌至 QR 碼](https://ironsoftware.com/csharp/barcode/how-to/customize-qr-code-style/)。 `IronDrawing`提供強大的圖像處理功能,與條碼處理功能相輔相成: ```csharp // 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); ``` 對於需要特定影像修正的情況,我們的 [ 影像修正指南](https://ironsoftware.com/csharp/barcode/how-to/image-correction/) 詳細說明如何使用篩選器來增強 BarCode 的可讀性。

為何選擇 `IronDrawing` 而非 `System.Drawing`? `IronDrawing` 與 `System.Drawing` 相比,具有令人信服的優勢: 1.**跨平台支援**:與 `System.Drawing` (Windows-only in .NET Core/5+) 不同,可在 Windows、Linux 和 macOS 上無縫運作。 2.**現代架構**:建基於 `SkiaSharp` 和 `ImageSharp` 以獲得更好的效能和記憶體管理。 3.**簡化的 API**:保留熟悉的 `System.Drawing` 類似介面,同時加入現代便利功能 4.**主動開發**:定期更新與改進,與維護模式中的 `System.Drawing` 不同 5.**更好的整合**:專為與 IronSoftware 產品達到最佳效能而設計 有關部署的注意事項,尤其是雲端環境,請參閱我們的 [ 部署到 Azure](https://ironsoftware.com/csharp/barcode/get-started/azure/) 和 [ 部署到 AWS](https://ironsoftware.com/csharp/barcode/get-started/aws/) 的指南,其中包含使用 `IronDrawing` 的跨平台相容性的特定注意事項。 無論是建立桌面應用程式、Web 服務或雲原生解決方案,`IronDrawing` 都能確保您的條碼處理程式碼在所有平台上保持可移植性和高效性,使其成為現代 .NET 開發的理想選擇。

常見問題解答

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

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

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

IronDrawing 是由 IronSoftware 創建的免費開放原始碼程式庫,提供跨平台的圖形操作支援。當您從 NuGet 安裝 IronBarcode 時,它會自動包含在內,並透過將系統.Drawing 物件轉換為相容的 AnyBitmap 格式,從所有作業系統上的 System.Drawing 物件啟用條碼讀取功能。

如何轉換 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,002,059 | 版本: 2025.12 剛發表