IronBarcode 如何使用 從 System.Drawing 讀取 BarCode 如何在 C# 中從 System.Drawing 讀取條碼 Hairil Hasyimi Bin Omar 更新:7月 22, 2025 下載 IronBarcode NuGet 下載 DLL 下載 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在雙子座打開 請向 Gemini 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 This article was translated from English: Does it need improvement? Translated View the article in English System.Drawing 物件在 .NET 中被開發人員廣泛用於與影像處理相關的任務。 然而,微軟已停止對其他作業系統(如MacOS和Linux)上的System.Drawing 提供支持,現在僅支援Windows 。 這項重大變更為在 Windows 以外的作業系統上使用 IronBarcode 的開發人員帶來了許多問題。 這是因為處理條碼通常涉及使用圖形、圖像和字體等物件。 為了解決這個問題,我們引入了一個替代方案: IronDrawing 。 這個由 IronSoftware 發起的免費**開源程式**庫旨在簡化在 Windows 以外的作業系統上執行該程式的過程。 這為我們的用戶提供了友善的用戶體驗。 從 NuGet 安裝 IronBarcode 後,IronDrawing 將自動包含在您的專案中。 快速入門:使用 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# 函式庫 利用 IronDrawing 將 System.Drawing 對象轉換為 AnyBitmap 使用Read方法從 AnyBitmap 物件讀取條碼 在控制台顯示檢測到的條碼值 探索另一篇文章以了解 IronDrawing 如何用於處理顏色和字體 系統轉換。繪製到 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) $vbLabelText $csharpLabel 從上面的程式碼片段中,我們載入了兩個條碼影像,分別是System.Drawing.Bitmap和System.Drawing.Image 。 然後,我們只需將它們賦值給AnyBitmap對象,即可隱式地將它們轉換為 AnyBitmap 類型。 隨後,我們將這些物件加入到AnyBitmap清單中。 從 AnyBitmap 讀取條碼 IronBarcode 的所有方法都可以直接接受IronSoftware.Drawing.AnyBitmap對象,而無需任何額外的配置。 這為使用 IronBarcode 和System.Drawing物件(Windows 以外的作業系統不支援這些物件)的開發人員提供了便利。 下面的程式碼片段示範如何實現這一點。 :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 上面的程式碼片段是對前一個程式碼片段的擴充。 一旦我們填入了 AnyBitmap 列表,我們就遍歷該列表,並將每個 AnyBitmap 物件作為參數呼叫Read方法,然後傳回IronBarcode.BarcodeResults 。 然後我們遍歷返回的對象,將條碼值列印到控制台。 IronSoftware.Drawing的功能範圍不僅限於鑄造影像。 它也被大量用於圖像處理的其他方面,例如用於條碼和二維碼樣式設計的顏色和字體。 用戶可以探索我們如何使用 IronDrawing 來客製化二維碼並添加徽標。 常見問題解答 如何在 .NET C# 中從 System.Drawing 物件讀取 BarCode? 您可以透過 IronBarcode 與 IronDrawing 結合使用,從 System.Drawing 物件讀取條碼。首先,使用 IronDrawing 將您的 System.Drawing 物件轉換成 AnyBitmap,然後再使用 IronBarcode 的 Read 方法來讀取條碼。 什麼是 IronDrawing,它對條碼讀取有什麼幫助? IronDrawing 是 IronSoftware 的免費開放源碼函式庫,可將 System.Drawing 物件隱式轉換為 AnyBitmap。透過使這些物件相容於 IronBarcode,方便在非 Windows 作業系統上讀取條碼。 我可以在 MacOS 和 Linux 上使用 IronBarcode 讀取條碼嗎? 是的,通過使用 IronDrawing,您可以將 System.Drawing 物件鑄造為 AnyBitmap,這使得 IronBarcode 可以在 MacOS 和 Linux 上讀取條碼,克服了 System.Drawing 僅限於 Windows 的限制。 哪些影像物件類型可以投射至 AnyBitmap 以進行 BarCode 讀取? 除了 System.Drawing 物件之外,您也可以將 System.Drawing.Bitmap、System.Drawing.Image、SkiaSharp.SKBitmap、SkiaSharp.SKImage 以及 SixLabors.ImageSharp 物件轉換為 AnyBitmap,以便使用 IronBarcode 讀取條碼。 如何使用 IronBarcode 顯示檢測到的條碼值? 使用 IronBarcode 的 Read 方法讀取條碼後,遍歷 BarcodeResult 陣列,並將每個條碼值列印到控制台。 從 NuGet 安裝條碼讀取庫時是否包含 IronDrawing? 是的,當您從 NuGet 安裝 IronBarcode 時,IronDrawing 會自動包含在您的專案中,提供條碼讀取的無縫整合。 圖像物件的隱式轉換如何幫助條碼處理? 使用 IronDrawing 將圖像物件隱含地轉換成 AnyBitmap,簡化了使 System.Drawing 物件與 IronBarcode 相容的過程,增強了各種作業系統上的條碼處理能力。 Hairil Hasyimi Bin Omar 立即與工程團隊聊天 軟體工程師 和所有优秀的工程师一样,Hairil 是个努力学习者。他正在细化自己的 C# 、Python 和 Java 知识,将这些知识应用于 Iron Software 各个团队成员以增加价值。Hairil 自马来西亚 Universiti Teknologi MARA 加入 Iron Software 团队,并以化学与工艺工程学士学位毕业。 準備好開始了嗎? Nuget 下載 1,979,979 | Version: 2025.11 剛發表 免費下載 NuGet 下載總數:1,979,979 檢視授權