IronBarcode 操作指南 從 System.Drawing 中讀取條碼 如何在 C# 中從System.Drawing讀取條碼 Hairil Hasyimi Bin Omar 更新:2026年1月10日 下載 IronBarcode NuGet 下載 DLL 下載 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 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 已停止在MacOS和Linux上支援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 建立 PDF 檔案: 使用 NuGet 套件管理器安裝 IronBarcode PM > Install-Package BarCode 複製並運行這段程式碼。 var results = IronBarCode.BarcodeReader.Read((AnyBitmap)(new System.Drawing.Bitmap("yourImage.png"))); 部署到您的生產環境進行測試 立即開始在您的專案中使用 IronBarcode,免費試用! 免費試用30天 ### 最小工作流程(5 個步驟) 從System.Drawing下載用於讀取條碼的 C# 函式庫 Utilize `IronDrawing` to cast `System.Drawing` objects into `AnyBitmap` Use the Read method to read barcodes from `AnyBitmap` objects 在控制台顯示檢測到的條碼值 Explore another article to learn how `IronDrawing` is used for handling color and fonts 我要如何將 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 請參閱此程式碼範例,以套用上述物件。 以下示範將條碼影像從 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); 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) $vbLabelText $csharpLabel 此程式碼透過 IronDrawing 展示 System.Drawing 物件與 IronBarcode 的無縫整合。 此相容性延伸至各種條碼格式,詳細資訊請參閱我們的 支援的條碼格式指南,包括 QR code、Code 128、Code 39 以及許多其他格式。 隱含式鑄造為何有效? 在上面的程式碼中,我們以 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 $vbLabelText $csharpLabel 哪些是常見的鑄造錯誤? 當將 System.Drawing 轉換為 AnyBitmap 時,開發人員可能會遇到以下問題: 1.空引用異常:在轉換之前確認您的 System.Drawing 物件不是 null 2.不支援的格式例外:某些特殊的影像格式需要預先轉換。 3.記憶體問題:大型圖片需要適當的處理模式 針對鑄造問題的疑難排解,我們的 疑難排解指南 提供條碼辨識過程中常見問題的解決方案。 如何從 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; 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 $vbLabelText $csharpLabel 除了基本的 Read 方法之外,IronBarcode 還提供了多種接受 AnyBitmap 參數的方法。 如需進階情況,請參閱我們的 讀取多個 BarCode 指南,展示如何有效率地處理單一影像中的多個 BarCode: // 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); ' 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) $vbLabelText $csharpLabel 如何處理多個 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 $vbLabelText $csharpLabel 我還可以使用哪些 IronDrawing 功能? IronSoftware.Drawing的功能不僅限於鑄造圖像。 它可處理影像處理方面的問題,例如對條碼和 QR 代碼造型有用的 色彩和 字體。 探索我們如何利用 IronDrawing 來 客製化及新增標誌至 QR 碼。 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); 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) $vbLabelText $csharpLabel 對於需要特定影像修正的情況,我們的 影像修正指南 詳細說明如何使用篩選器來增強 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.更佳的整合性:專為與 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,070,733 | 版本: 2026.2 剛剛發布 免費 NuGet 下載 總下載量:2,070,733 查看許可證