條碼 .NET 元件教程使用 IronBarcode
條碼 .NET 元件是一個託管程式碼庫,只需幾行程式碼即可在 C# 應用程式中產生和讀取條碼。 IronBarcode支援所有主流條碼——Code 128、QR、Data Matrix、EAN、UPC 等——並且可在 Windows、Linux 和 macOS 上運行,無需額外的運行時依賴項。
透過 NuGet 安裝,即可在幾分鐘內開始產生條碼:
Install-Package BarCode
Install-Package BarCode
什麼是條碼 .NET 元件?
條碼 .NET 元件是一個以 NuGet 套件形式打包的軟體庫,它透過簡潔的 API 提供條碼產生和讀取功能。 與需要手動計算校驗和以及複雜格式規則的條碼字體不同,專用元件會在內部處理所有編碼邏輯。
IronBarcode庫提供了兩個主要入口點:
BarcodeWriter-- 從文字或數位資料建立條碼影像、PDF 和 HTMLBarcodeReader-- 掃描影像、PDF 和多幀 TIFF 檔案以提取條碼值
這種雙向設計意味著您既可以將條碼列印到標籤上,也可以從同一個庫中的文件中掃描條碼,這對於庫存管理、文件追蹤、Crystal Reports 整合和資料自動化工作流程至關重要。
支援的符號體系包括:
-一維線性條碼:Code 128、Code 39、Code 93、ITF-14、EAN-13、EAN-8、UPC-A、UPC-E -二維矩陣:QR碼、Data Matrix碼、PDF417碼、Aztec碼
- GS1 變體:GS1-128、GS1 DataBar
製造系統通常使用 Code 128 進行產品跟踪,因為它能有效地對字母數字資料進行編碼。 醫療保健應用通常使用資料矩陣字體進行藥品標籤印刷,因為它在非常小的尺寸下也能很好地列印。 零售銷售點系統在結帳時掃描 EAN-13 和 UPC-A 條碼。 在整合條碼功能時,選擇適合您使用場景的符號系統是您需要做出的首要決定之一。
如何在.NET專案中安裝IronBarcode?
使用 Visual Studio 中的 NuGet 套件管理器或 .NET CLI 安裝 IronBarcode 只需不到兩分鐘。
套件管理器控制台(Visual Studio):
Install-Package BarCode
Install-Package BarCode
.NET CLI :
dotnet add package BarCode
dotnet add package BarCode
安裝軟體包後,在任何使用條碼功能的檔案頂部新增命名空間:
using IronBarCode;
using IronBarCode;
Imports IronBarCode
IronBarcode 的目標框架為 .NET Framework 4.6.2 及更高版本、.NET Core 3.1 及更高版本以及 .NET 5 至 .NET 10。它可在 Windows、Linux 和 macOS 上運行,因此相同的程式碼無需特定於平台的配置即可在雲端託管容器、本機伺服器和開發人員工作站上運行。
對於 ASP.NET Core 應用程序,無需註冊中間件。 您可以直接從控制器、後台服務或 Razor Pages 呼叫 API。 Windows Forms 和 WPF 應用程式以相同的方式獲得存取權限——新增命名空間並開始呼叫方法。
如何在 C# 中整合條碼 .NET 元件:圖 4 - 產生具有跨平台支援的條碼
IronBarcode 入門指南中提供了詳細的安裝步驟,包括離線 NuGet 來源設定和代理設定。
如何在C#中產生條碼影像?
使用 IronBarcode 產生條碼需要三個步驟:選擇編碼、設定值、儲存輸出。 以下範例建立 Code 128 條碼,並將其儲存為 PNG 映像和 PDF 文件:
using IronBarCode;
// Create a Code 128 barcode encoding a product identifier
var barcode = BarcodeWriter.CreateBarcode("PRD-12345-2024", BarcodeEncoding.Code128);
// Set the output dimensions in pixels
barcode.ResizeTo(400, 100);
// Add human-readable text beneath the bars
barcode.AddBarcodeValueTextBelowBarcode();
// Export to PNG for screen display and label printing
barcode.SaveAsImage("product-barcode.png");
// Export to PDF for document embedding
barcode.SaveAsPdf("product-barcode.pdf");
using IronBarCode;
// Create a Code 128 barcode encoding a product identifier
var barcode = BarcodeWriter.CreateBarcode("PRD-12345-2024", BarcodeEncoding.Code128);
// Set the output dimensions in pixels
barcode.ResizeTo(400, 100);
// Add human-readable text beneath the bars
barcode.AddBarcodeValueTextBelowBarcode();
// Export to PNG for screen display and label printing
barcode.SaveAsImage("product-barcode.png");
// Export to PDF for document embedding
barcode.SaveAsPdf("product-barcode.pdf");
Imports IronBarCode
' Create a Code 128 barcode encoding a product identifier
Dim barcode = BarcodeWriter.CreateBarcode("PRD-12345-2024", BarcodeEncoding.Code128)
' Set the output dimensions in pixels
barcode.ResizeTo(400, 100)
' Add human-readable text beneath the bars
barcode.AddBarcodeValueTextBelowBarcode()
' Export to PNG for screen display and label printing
barcode.SaveAsImage("product-barcode.png")
' Export to PDF for document embedding
barcode.SaveAsPdf("product-barcode.pdf")
ResizeTo() 方法可以精確設定像素尺寸,這在以特定 DPI 列印標籤時非常重要。 AddBarcodeValueTextBelowBarcode() 呼叫會在條形圖下方添加人類可讀的文本,使倉庫和零售環境中的人工驗證更加容易。
條碼輸出格式
如何在 C# 中整合條碼 .NET 元件:圖 5 - PNG 輸出
如何在 C# 中整合條碼 .NET 元件:圖 6 - PDF 輸出
IronBarcode 可匯出為 PNG、JPEG、GIF、TIFF、BMP、PDF、HTML 和 base64 字串。 base64 格式特別適用於將條碼嵌入到 API 回應中,前端可以動態渲染這些回應,而無需將檔案寫入磁碟。
如何在C#中產生二維碼?
對於二維碼,IronBarcode 提供了 QRCodeWriter 類,該類別內建了糾錯層級和模組大小控制的支援:
using IronBarCode;
// Generate a QR code from a URL with 500px dimensions
var qrCode = QRCodeWriter.CreateQrCode("https://example.com/product/12345", 500);
// Save the QR code to a PNG file
qrCode.SaveAsImage("product-qr.png");
using IronBarCode;
// Generate a QR code from a URL with 500px dimensions
var qrCode = QRCodeWriter.CreateQrCode("https://example.com/product/12345", 500);
// Save the QR code to a PNG file
qrCode.SaveAsImage("product-qr.png");
Imports IronBarCode
' Generate a QR code from a URL with 500px dimensions
Dim qrCode = QRCodeWriter.CreateQrCode("https://example.com/product/12345", 500)
' Save the QR code to a PNG file
qrCode.SaveAsImage("product-qr.png")
二維碼可以編碼網址、純文字、vCard聯絡人資料、Wi-Fi憑證和任意位元組序列。 條碼產生範例頁面涵蓋了其他編碼場景,包括 Data Matrix 和 PDF417。
如何從影像中讀取條碼?
從影像中讀取條碼與寫入條碼一樣直接。 BarcodeReader.Read() 方法接受檔案路徑、Stream、Bitmap 或 IronSoftware.Drawing.AnyBitmap,並傳回一個 BarcodeResults 集合:
using IronBarCode;
// Read all barcodes from a scanned document image
BarcodeResults results = BarcodeReader.Read("scanned-document.png");
// Iterate over every detected barcode
foreach (BarcodeResult result in results)
{
string value = result.Value;
BarcodeEncoding type = result.BarcodeType;
Console.WriteLine($"Detected {type}: {value}");
}
using IronBarCode;
// Read all barcodes from a scanned document image
BarcodeResults results = BarcodeReader.Read("scanned-document.png");
// Iterate over every detected barcode
foreach (BarcodeResult result in results)
{
string value = result.Value;
BarcodeEncoding type = result.BarcodeType;
Console.WriteLine($"Detected {type}: {value}");
}
Imports IronBarCode
' Read all barcodes from a scanned document image
Dim results As BarcodeResults = BarcodeReader.Read("scanned-document.png")
' Iterate over every detected barcode
For Each result As BarcodeResult In results
Dim value As String = result.Value
Dim type As BarcodeEncoding = result.BarcodeType
Console.WriteLine($"Detected {type}: {value}")
Next
閱讀器會自動對影像進行預處理,以修正常見的掃描問題:旋轉、傾斜、雜訊、低對比度和透視變形。 這意味著您可以直接傳遞原始掃描器輸出,而無需自行進行預處理。
從真實文件掃描
讀取輸出
如何配置條碼讀取選項?
對於大批量或具有挑戰性的掃描場景,BarcodeReaderOptions 可讓您對讀取演算法進行精細控制:
using IronBarCode;
var options = new BarcodeReaderOptions
{
// Balance accuracy and processing time
Speed = ReadingSpeed.Balanced,
// Detect multiple barcodes in one pass
ExpectMultipleBarcodes = true,
// Limit the search to 1D formats for faster processing
ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional
};
// Read from a multi-page PDF warehouse inventory report
var results = BarcodeReader.Read("warehouse-inventory.pdf", options);
using IronBarCode;
var options = new BarcodeReaderOptions
{
// Balance accuracy and processing time
Speed = ReadingSpeed.Balanced,
// Detect multiple barcodes in one pass
ExpectMultipleBarcodes = true,
// Limit the search to 1D formats for faster processing
ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional
};
// Read from a multi-page PDF warehouse inventory report
var results = BarcodeReader.Read("warehouse-inventory.pdf", options);
Imports IronBarCode
Dim options As New BarcodeReaderOptions With {
.Speed = ReadingSpeed.Balanced,
.ExpectMultipleBarcodes = True,
.ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional
}
' Read from a multi-page PDF warehouse inventory report
Dim results = BarcodeReader.Read("warehouse-inventory.pdf", options)
將 ExpectBarcodeTypes 設定為與您實際期望的格式匹配可以提高吞吐量,因為讀取器會跳過無法匹配的模式。 Speed 屬性的範圍從 Faster(最適合乾淨、高品質的圖像)到 ExtremeDetail(最適合損壞或低解析度的來源)。 條碼讀取文件涵蓋了其他選項,包括多線程和感興趣區域掃描。
如何自訂條碼外觀?
IronBarcode 讓您在儲存條碼之前控制產生的條碼的每個視覺方面。 您可以透過程式設定顏色、字體、邊距和註釋文字。 在為品牌標籤或受監管行業產生條碼時,如果標籤佈局遵循嚴格的規範,這將非常有用。
此 API 遵循流暢式模式:建立條碼,呼叫樣式方法,然後儲存。 所有樣式屬性都會立即影響輸出,無需單獨的渲染步驟。有關可用屬性的完整列表,請參閱樣式和註釋範例。
如何處理PDF文件中的條碼?
IronBarcode 讀取嵌入在 PDF 檔案中的條碼的方式與讀取影像檔案的方式相同。 將 PDF 路徑傳遞給 BarcodeReader.Read(),該程式庫會自動從每一頁中提取條碼。 這對於應付帳款工作流程(其中發票 PDF 包含 GS1-128 條碼)和物流系統(其中出貨清單以 PDF 附件形式分發)來說很有價值。
您也可以使用 IronBarcode 和IronPDF直接在 PDF 頁面中寫入條碼以產生文件。 常見的做法是產生一個包含人可讀位址區塊和用於追蹤號碼的可掃描 Code 128 條碼的貨運標籤 PDF。
如需進一步了解 PDF 條碼工作流程, IronBarcode 教學部分提供了涵蓋發票處理、大量標籤列印和文件歸檔的端對端範例。
如何將條碼整合到 ASP.NET Core API 中?
從 ASP.NET Core 控制器端點傳回條碼影像是顯示動態標籤的 Web 入口網站的常見需求。 IronBarcode 的 base64 輸出讓這一切變得很簡單:
using IronBarCode;
// In an ASP.NET Core controller action
public IActionResult GetBarcodeImage(string productId)
{
var barcode = BarcodeWriter.CreateBarcode(productId, BarcodeEncoding.Code128);
barcode.ResizeTo(400, 100);
barcode.AddBarcodeValueTextBelowBarcode();
// Return the barcode as a PNG image response
byte[] imageBytes = barcode.ToJpegBinaryData();
return File(imageBytes, "image/jpeg");
}
using IronBarCode;
// In an ASP.NET Core controller action
public IActionResult GetBarcodeImage(string productId)
{
var barcode = BarcodeWriter.CreateBarcode(productId, BarcodeEncoding.Code128);
barcode.ResizeTo(400, 100);
barcode.AddBarcodeValueTextBelowBarcode();
// Return the barcode as a PNG image response
byte[] imageBytes = barcode.ToJpegBinaryData();
return File(imageBytes, "image/jpeg");
}
Imports IronBarCode
' In an ASP.NET Core controller action
Public Function GetBarcodeImage(productId As String) As IActionResult
Dim barcode = BarcodeWriter.CreateBarcode(productId, BarcodeEncoding.Code128)
barcode.ResizeTo(400, 100)
barcode.AddBarcodeValueTextBelowBarcode()
' Return the barcode as a PNG image response
Dim imageBytes As Byte() = barcode.ToJpegBinaryData()
Return File(imageBytes, "image/jpeg")
End Function
這種方法適用於任何前端框架。 瀏覽器收到標準影像回應,並將其顯示在 <img> 標籤中。 對於高流量端點,只需產生一次條碼並快取位元組數組,因為相同的產品 ID 總是產生相同的條碼圖像。
IronBarcode API 參考文件記錄了所有可用方法,包括串流輸出,從而避免為非常高解析度的條碼分配大型位元組數組。
IronBarcode如何處理條碼驗證和糾錯?
條碼標準定義了嚴格的編碼規則和校驗和要求。 Code 128 使用加權模 103 校驗和。 EAN-13 使用模 10。二維碼嵌入了里德-所羅門糾錯技術,即使條碼高達 30% 被遮蔽或損壞,也能部分恢復資料。
IronBarcode 會自動執行這些規則。 當您呼叫 BarcodeWriter.CreateBarcode() 時,該函式庫會驗證資料是否符合符號系統的字元集和長度約束,然後計算並附加正確的校驗和,而無需您的程式碼執行任何其他步驟。 這樣可以防止產生掃描器會拒絕的無效條碼。
在讀取方面,該庫在解碼過程中應用了糾錯功能,這意味著它通常可以從部分撕裂、污損或低解析度列印的條碼中恢復正確的值。 這種行為對於零售和物流行業的GS1 條碼標準合規性尤其重要。
若要深入了解條碼糾錯在規範層面的工作原理, ISO/IEC 條碼標準文件提供了權威的技術參考。
條碼整合的常見場景有哪些?
| 情境 | 符號系統 | IronBarcode 的主要功能 |
|---|---|---|
| 零售銷售點 | EAN-13,UPC-A | BarcodeEncoding.EAN13 ,PDF匯出 |
| 倉庫庫存 | 代碼 128,ITF-14 | ExpectMultipleBarcodes = true |
| 醫療保健標籤 | 資料矩陣,GS1-128 | 小尺寸列印,高解析度導出 |
| 文件追蹤 | PDF417,代碼 39 | PDF 閱讀,支援多頁 |
| 移動產品查找 | QR碼 | QRCodeWriter.CreateQrCode() |
| 運輸標籤 | 代碼 128,GS1-128 | 標籤大小的 PDF 輸出,文字註釋 |
不同的場景適合不同的配置選擇。 IronBarcode 範例庫提供了上述所有場景的可運行程式碼。
下一步計劃是什麼?
整合式條碼 .NET 元件可以將原本需要數週時間的自訂開發工作簡化為一個下午的設定工作。 IronBarcode 處理編碼規則、校驗和、影像預處理和糾錯,因此您的團隊可以專注於條碼工作流程的業務邏輯,而不是條碼機製本身。
繼續前進:
- 在軟體包管理器控制台中安裝軟體包:
dotnet add package BarCode或Install-Package BarCode2.嘗試快速入門:條碼快速入門範例僅需不到 20 行程式碼即可完成條碼的產生和讀取。 3.探索條碼符號體系:支援的條碼類型參考清單列出了所有編碼格式及使用指南。 4.審核定價: IronBarcode 的授權選項涵蓋單一開發者、團隊和 OEM 再分發用例。 5.開始免費試用:下載30 天免費試用許可證,在您自己的應用程式中評估全部功能。
對於企業許可、大量部署或技術整合難題等問題, IronBarcode 支援團隊隨時為您提供協助。
條碼標準和規範的外部參考資料:
GS1條碼標準-全球零售與物流條碼規範權威機構
- ISO/IEC 條碼標準委員會-一維與二維條碼符號系統的國際標準 Stack Overflow 上的 C# 條碼問答-- 關於 .NET 中條碼實現的社群討論
常見問題解答
什麼是條碼 .NET 元件?
條碼.NET元件是一個軟體程式庫,讓開發者能夠將條碼生成與掃描整合到.NET應用程式中,自動處理編碼規則、校驗和、及圖像預處理。
IronBarcode 如何在 .NET 應用程序中提供幫助?
IronBarcode提供一個.NET元件,透過簡單的API生成和讀取條碼,使您能輕鬆地將條碼功能新增到C#應用程式中,而無需手動實作編碼算法。
使用IronBarcode可以生成哪些類型的條碼?
IronBarcode支援Code 128、Code 39、Code 93、ITF-14、EAN-13、EAN-8、UPC-A、UPC-E、QR Code、Data Matrix、PDF417、Aztec和GS1變體。
為什麼我應該使用條碼元件而不是自己創建解決方案?
專業的條碼元件可以自動處理校驗和計算、錯誤更正、圖像預處理和多格式支援,減少開發時間及生成無效條碼的風險。
IronBarcode 適合數據自動化任務嗎?
是的,IronBarcode非常適合數據自動化。它能從圖像和PDF中讀取條碼,並直接整合到背景服務、排程任務和ASP.NET Core API中。
IronBarcode 可以用於文件跟踪嗎?
是的。IronBarcode可以從多頁PDF檔案和多幀TIFF中讀取條碼,使其在物流、應付帳款和記錄管理中的文件追蹤工作流程中實用。
IronBarcode支持哪些.NET版本?
IronBarcode支援.NET Framework 4.6.2及以上版本、.NET Core 3.1及以上版本,以及.NET 5至.NET 10,可在Windows、Linux和macOS上運行。
IronBarcode 如何提升庫存管理系統?
IronBarcode提供從圖像和PDF可靠的條碼生成和多條碼掃描,讓倉庫和零售環境中的庫存追蹤快速而精確。

