條碼 .NET 元件教程使用 IronBarcode
條碼.NET組件是一個託管程式碼程式庫,讓您僅需幾行程式碼即可在C#應用程式中生成和讀取條碼。 IronBarcode 支援所有主要符號學——Code 128、QR、Data Matrix、EAN、UPC等等——並能在Windows、Linux和macOS上運行,而不需要額外的運行時依賴。
通過NuGet安裝後,可在幾分鐘內開始生成條碼:
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 Code、Data Matrix、PDF417、Aztec
- GS1 變體:GS1-128、GS1 DataBar
製造系統通常使用Code 128進行產品追蹤,因為它能有效編碼字母數字資料。 醫療應用通常依賴於Data Matrix進行藥物標籤,因為它可以在非常小的尺寸下良好列印。 零售POS系統在結賬時會掃描EAN-13和UPC-A條碼。 在整合條碼功能時,為您的用例選擇正確的符號學是您將做出的首批決策之一。
如何在.NET專案中安裝IronBarcode?
使用Visual Studio中的NuGet包管理器或.NET CLI安裝IronBarcode不到兩分鐘。
包管理器控制台(Visual Studio):
Install-Package BarCode
.NET CLI:
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頁面直接調用API。 Windows Forms和WPF應用程式通過新增命名空間並開始調用方法獲得相同存取。

詳細的安裝步驟,包括離線NuGet飼料配置和代理設定,可以在IronBarcode快速入門指南中找到。
您如何在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()調用在條形下新增可讀文字,使倉庫和零售環境中的手動驗證變得更容易。
條碼輸出格式


IronBarcode匯出為PNG、JPEG、GIF、TIFF、BMP、PDF、HTML和base64字串。 base64格式特別適用於在API響應中嵌入條碼,前端動態呈現,而無需將文件寫入磁碟。
您如何在C#中生成QR Code?
對於QR Code,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")
QR Code可以編碼URL、純文字、vCard聯絡資料、Wi-Fi憑據和任意字節序列。 條碼生成範例頁面涵蓋了更多編碼場景,包括Data Matrix和PDF417。
您如何從圖像中讀取條碼?
從圖像讀取條碼就像寫條碼一樣直接。 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以匹配您實際期望的格式可提高吞吐量,因為讀取器將跳過不可能匹配的模式。 ExtremeDetail(最佳處理受損或低解析度來源)。 條碼讀取文件涵蓋了更多選項,包括多執行緒和感興趣區域掃描。
您如何自訂條碼外觀?
IronBarcode讓您能在保存之前控制生成條碼的每個視覺元素。 您可以以程式化方式設置顏色、字體、邊距和註釋文字。 這在生成品牌標籤或遵循嚴格格式規範的受管制行業中特別有用。
API採用流利風格的模式:建立條碼、調用樣式方法,然後保存。 所有樣式屬性立即影響輸出,不需要單獨的渲染步驟。 查看樣式和註釋範例以獲取可用屬性的完整列表。
您如何處理PDF文件中的條碼?
IronBarcode讀取嵌入在PDF文件中的條碼的方法與讀取圖像文件相同。 將PDF路徑傳遞給BarcodeReader.Read(),程式庫會自動從每頁提取條碼。 這對於應付帳款流程(發票PDF中包含GS1-128條碼)和物流系統(運送清單用作PDF附件分發)都很有價值。
您還可以使用IronBarcode和IronPDF來直接將條碼寫入PDF頁面進行文件生成。 常見模式是生成一個運送標籤PDF,其中包含一個人可讀的地址塊和一個可掃描的Code 128條碼作為跟蹤號碼。
對於PDF條碼工作流程的進一步閱讀,IronBarcode教程部分提供涵蓋發票處理、批量標籤列印和文件歸檔的端到端範例。
您如何整合條碼到ASP.NET Core API中?
從ASP.NET Core控制器端點返回條碼圖像是顯示動態標籤的網路門戶的一個常見需求。 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。QR Code嵌入Reed-Solomon錯誤更正,即使條碼最多遮擋或損壞了30%也允許部分資料恢復。
IronBarcode會自動執行這些規則。 當您調用BarcodeWriter.CreateBarcode()時,程式庫會驗證資料是否符合符號學的字元集和長度限制,然後計算並附加正確的校驗碼,而不需要您的程式碼進行任何其他步驟。 這可以防止生成掃描器會拒絕的無效條碼。
在讀取方面,程式庫在解碼時應用錯誤更正,這意味著它通常可以從部分撕裂、弄污或以低解析度列印的條碼中恢復正確的值。 對於零售和物流中的GS1條碼標準 合規性來說,這種行為尤為重要。
欲深入了解條碼錯誤更正在規範層次上的工作方式,ISO/IEC條碼標準文件提供權威的技術參考。
常見的條碼整合場景有哪些?
| 場景 | 符號學 | IronBarcode的主要功能 |
|---|---|---|
| 零售POS | EAN-13, UPC-A | BarcodeEncoding.EAN13, PDF export |
| 倉庫庫存 | Code 128, ITF-14 | ExpectMultipleBarcodes = true |
| 醫療標籤 | Data Matrix, GS1-128 | 小尺寸列印,高DPI匯出 |
| 文件跟踪 | PDF417, Code 39 | PDF閱讀,多頁支持 |
| 移動產品查找 | QR Code | QRCodeWriter.CreateQrCode() |
| 運送標籤 | Code 128, GS1-128 | 標籤尺寸PDF輸出,文字註釋 |
每個場景都從不同的配置選擇中受益。 IronBarcode範例集錦 提供了所有上述場景的可執行程式碼。
您的下一步是什麼?
整合一個條碼.NET組件會將原本需要數周的客製化開發轉變為一個下午的配置。 IronBarcode處理編碼規則、校驗碼、圖像預處理和錯誤更正,因此您的團隊可以專注於條碼工作流程相關的業務邏輯而不是條碼機制本身。
為了前進:
- 安裝包: 在包管理器控制台中使用
dotnet add package BarCode或Install-Package BarCode - 嘗試快速入門: 條碼快速入門範例 在20行以下的程式碼中提供生成和讀取的指導
- 探索符號學:支援的條碼型別參考列出了所有編碼格式的使用指南
- 檢視定價:IronBarcode授權選項涵蓋單一開發者、團隊及OEM再分發用例
- 開始免費試用:下載30天免費試用授權以評估您自己應用程式的完整功能集
關於企業授權、高容量部署或技術整合挑戰的問題,IronBarcode支持團隊隨時可幫助您。
條碼標準和規範的外部參考:
- GS1條碼標準——全球零售和物流條碼規範的權威機構
- ISO/IEC條碼標準委員會——1D和2D條碼符號學的國際標準
- C#條碼在Stack Overflow上的問答——有關.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碼、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中多條碼掃描,於倉庫及零售環境中提供迅速且準確的庫存追蹤。




