如何在 C# .NET 應用程式中條碼生成(建立條碼圖像完整教學)
需要在.NET應用程式中快速產生專業條碼圖像嗎? 本教學將向您展示如何使用IronBarcode來建立、自訂和匯出條碼——從簡單的單行實現到進階樣式技術,讓您可以完全控制條碼的外觀。
快速入門:立即建立並儲存條碼影像
使用IronBarcode ,只需一次簡單的呼叫即可產生和匯出條碼影像。 使用 CreateBarcode 方法輸入文本,選擇格式和大小,然後呼叫 SaveAsPng — 無需複雜的設定。
最簡工作流程(5個步驟)
- 透過NuGet套件管理器安裝IronBarcode
- 用一行程式碼產生一個簡單的條碼
- 為條碼套用自訂樣式和註釋
- 將條碼匯出為圖像、PDF 或 HTML 格式
- 使用 Fluent API 實現高效條碼生成
如何在 C# 中安裝條碼產生器函式庫?
使用NuGet套件管理器安裝IronBarcode只需幾秒鐘。 您可以直接透過套件管理器控制台安裝,也可以手動下載 DLL 檔案。
Install-Package BarCode
*IronBarcode為.NET開發人員提供全面的條碼產生功能。*
如何使用 C# 產生簡單的條碼?
創建你的第一個條碼只需要兩行程式碼。 下面的範例示範如何產生標準 Code128 條碼並將其儲存為影像檔案。
using IronBarCode;
// Create a barcode with your desired content and encoding type
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.Code128);
// Save the barcode as a PNG image file
myBarcode.SaveAsPng("myBarcode.png");
// Optional: Open the generated image in your default viewer
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("myBarcode.png") { UseShellExecute = true });
using IronBarCode;
// Create a barcode with your desired content and encoding type
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.Code128);
// Save the barcode as a PNG image file
myBarcode.SaveAsPng("myBarcode.png");
// Optional: Open the generated image in your default viewer
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("myBarcode.png") { UseShellExecute = true });
Imports IronBarCode
' Create a barcode with your desired content and encoding type
Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.Code128)
' Save the barcode as a PNG image file
myBarcode.SaveAsPng("myBarcode.png")
' Optional: Open the generated image in your default viewer
System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo("myBarcode.png") With {.UseShellExecute = True})
BarcodeWriter.CreateBarcode() 方法是您產生條碼的入口點。 它接受兩個參數:要編碼的資料和來自 BarcodeWriterEncoding 枚舉的條碼格式。 IronBarcode支援所有主流條碼格式,包括 Code128、Code39、EAN13、UPC-A、PDF417、DataMatrix 和 QR 碼。
生成後,GeneratedBarcode 物件提供多種匯出選項。 您可以將其儲存為各種影像格式(PNG、JPEG、GIF、TIFF),匯出為 PDF,甚至可以將其擷取為 System.Drawing.Bitmap 以便在您的應用程式中進行進一步處理。
*A Code128 barcode generated with IronBarcode displaying a URL*
我可以自訂產生的條碼的外觀嗎?
IronBarcode提供豐富的自訂選項,遠遠超出基本的條碼產生功能。 您可以新增註解、調整顏色、設定邊距,並控制條碼外觀的各個方面。
using IronBarCode;
using IronSoftware.Drawing;
// Create a QR code with advanced styling options
GeneratedBarcode myBarCode = BarcodeWriter.CreateBarcode(
"https://ironsoftware.com/csharp/barcode",
BarcodeWriterEncoding.QRCode
);
// Add descriptive text above the barcode
myBarCode.AddAnnotationTextAboveBarcode("Product URL:");
// Display the encoded value below the barcode
myBarCode.AddBarcodeValueTextBelowBarcode();
// Set consistent margins around the barcode
myBarCode.SetMargins(100);
// Customize the barcode color (purple in this example)
myBarCode.ChangeBarCodeColor(Color.Purple);
// Export as an HTML file for web integration
myBarCode.SaveAsHtmlFile("MyBarCode.html");
using IronBarCode;
using IronSoftware.Drawing;
// Create a QR code with advanced styling options
GeneratedBarcode myBarCode = BarcodeWriter.CreateBarcode(
"https://ironsoftware.com/csharp/barcode",
BarcodeWriterEncoding.QRCode
);
// Add descriptive text above the barcode
myBarCode.AddAnnotationTextAboveBarcode("Product URL:");
// Display the encoded value below the barcode
myBarCode.AddBarcodeValueTextBelowBarcode();
// Set consistent margins around the barcode
myBarCode.SetMargins(100);
// Customize the barcode color (purple in this example)
myBarCode.ChangeBarCodeColor(Color.Purple);
// Export as an HTML file for web integration
myBarCode.SaveAsHtmlFile("MyBarCode.html");
Imports IronBarCode
Imports IronSoftware.Drawing
' Create a QR code with advanced styling options
Private myBarCode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.QRCode)
' Add descriptive text above the barcode
myBarCode.AddAnnotationTextAboveBarcode("Product URL:")
' Display the encoded value below the barcode
myBarCode.AddBarcodeValueTextBelowBarcode()
' Set consistent margins around the barcode
myBarCode.SetMargins(100)
' Customize the barcode color (purple in this example)
myBarCode.ChangeBarCodeColor(Color.Purple)
' Export as an HTML file for web integration
myBarCode.SaveAsHtmlFile("MyBarCode.html")
GeneratedBarcode 類別提供了一組豐富的自訂方法:
-註:使用 AddAnnotationTextAboveBarcode() 和 AddAnnotationTextBelowBarcode() 在條碼周圍新增自訂標籤或說明。
-值顯示:AddBarcodeValueTextBelowBarcode() 方法會自動以人類可讀格式顯示編碼資料。
-間距:使用 SetMargins() 控制空白,以確保正確掃描和視覺效果。
-顏色:使用 ChangeBarCodeColor() 和 ChangeBackgroundColor() 變更前景色和背景色
匯出選項:儲存為影像檔案、PDF 或獨立的 HTML 文件
*帶有自訂顏色和註釋文字的樣式化二維碼*有關詳細的自訂選項,請瀏覽GeneratedBarcode 類文檔,其中涵蓋了所有可用的樣式方法和屬性。
如何用一行程式碼建立並匯出條碼?
IronBarcode實現了一種流暢的 API 設計模式,支援方法鏈,從而編寫出更簡潔、更易讀的程式碼。 當需要對條碼進行多次轉換時,這種方法尤其有用。
using IronBarCode;
using IronSoftware.Drawing;
// Generate, style, and convert a barcode in a single statement
string value = "https://ironsoftware.com/csharp/barcode";
// Create PDF417 barcode with chained operations
AnyBitmap barcodeBitmap = BarcodeWriter
.CreateBarcode(value, BarcodeWriterEncoding.PDF417) // Create PDF417 barcode
.ResizeTo(300, 200) // Set specific dimensions
.SetMargins(10) // Add 10px margins
.ToBitmap(); // Convert to bitmap
// Convert to System.Drawing.Bitmap for legacy compatibility
System.Drawing.Bitmap legacyBitmap = barcodeBitmap;
using IronBarCode;
using IronSoftware.Drawing;
// Generate, style, and convert a barcode in a single statement
string value = "https://ironsoftware.com/csharp/barcode";
// Create PDF417 barcode with chained operations
AnyBitmap barcodeBitmap = BarcodeWriter
.CreateBarcode(value, BarcodeWriterEncoding.PDF417) // Create PDF417 barcode
.ResizeTo(300, 200) // Set specific dimensions
.SetMargins(10) // Add 10px margins
.ToBitmap(); // Convert to bitmap
// Convert to System.Drawing.Bitmap for legacy compatibility
System.Drawing.Bitmap legacyBitmap = barcodeBitmap;
Imports IronBarCode
Imports IronSoftware.Drawing
' Generate, style, and convert a barcode in a single statement
Private value As String = "https://ironsoftware.com/csharp/barcode"
' Create PDF417 barcode with chained operations
Private barcodeBitmap As AnyBitmap = BarcodeWriter.CreateBarcode(value, BarcodeWriterEncoding.PDF417).ResizeTo(300, 200).SetMargins(10).ToBitmap() ' Convert to bitmap
' Convert to System.Drawing.Bitmap for legacy compatibility
Private legacyBitmap As System.Drawing.Bitmap = barcodeBitmap
流暢式 API 模式具有以下幾個優點:
-可讀性:將操作以邏輯順序串聯起來,使其讀起來像自然語言。 -效率:減少變數宣告與中間步驟 -靈活性:無需重構程式碼即可輕鬆新增或刪除操作
常見的流暢操作包括:
ResizeTo(): 精確控制條碼尺寸SetMargins(): 新增一致間距ChangeBarCodeColor(): 修改外觀AddAnnotationTextAboveBarcode(): 包含描述性文本SaveAsPdf():匯出為多種格式
*A PDF417 barcode generated using fluent method chaining*
IronBarcode支援哪些條碼格式?
IronBarcode透過 BarcodeWriterEncoding 枚舉支援全面的條碼格式產生。 支援的格式包括:
一維條碼:Code128、Code39、Code93、Codabar、ITF、MSI、Plessey、UPCA、UPCE、EAN8、EAN13
二維條碼:QR碼、DataMatrix碼、PDF417碼、Aztec碼、MaxiCode碼
特殊格式:IntelligentMail、DataBar、DataBarExpanded 和各種 GS1 標準
每種格式都有其特定的特點和應用場景。 例如,二維碼擅長儲存網址和大量數據,而 EAN13 是零售產品的標準。 了解更多關於如何為您的應用程式選擇合適的條碼格式的資訊。
如何驗證我產生的條碼是否可讀?
品質保證對於條碼的實施至關重要。 IronBarcode內建驗證功能,可確保您產生的條碼始終可掃描:
// Generate and verify a barcode
GeneratedBarcode myBarcode = BarcodeWriter
.CreateBarcode("TEST123", BarcodeWriterEncoding.Code128)
.ResizeTo(200, 100)
.ChangeBarCodeColor(Color.DarkBlue);
// Verify the barcode is still readable after modifications
bool isReadable = myBarcode.Verify();
Console.WriteLine($"Barcode verification: {(isReadable ? "PASS" : "FAIL")}");
// Generate and verify a barcode
GeneratedBarcode myBarcode = BarcodeWriter
.CreateBarcode("TEST123", BarcodeWriterEncoding.Code128)
.ResizeTo(200, 100)
.ChangeBarCodeColor(Color.DarkBlue);
// Verify the barcode is still readable after modifications
bool isReadable = myBarcode.Verify();
Console.WriteLine($"Barcode verification: {(isReadable ? "PASS" : "FAIL")}");
' Generate and verify a barcode
Dim myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("TEST123", BarcodeWriterEncoding.Code128).ResizeTo(200, 100).ChangeBarCodeColor(Color.DarkBlue)
' Verify the barcode is still readable after modifications
Dim isReadable As Boolean = myBarcode.Verify()
Console.WriteLine($"Barcode verification: {(If(isReadable, "PASS", "FAIL"))}")
Verify() 方法檢查條碼在套用調整大小或重新著色等轉換後是否仍可讀取機器。 當使用非標準顏色或非常小的尺寸時,這一點尤其重要。
哪裡可以找到更多條碼生成範例?
若要擴充條碼產生功能,請探索以下其他資源:
原始碼和範例
下載本教學的完整原始碼:
高級主題
-產生帶有徽標的二維碼- 為您的二維碼添加品牌標識
條碼樣式指南- 掌握進階自訂技巧
-從影像中讀取條碼- 透過條碼掃描完成循環
-批次產生條碼- 高效率產生多個條碼
API 文件
BarcodeWriter類別參考- 完整的方法文檔GeneratedBarcode類別參考- 所有自訂選項BarcodeWriterEncoding枚舉- 支援的條碼格式
準備好在您的應用程式中產生專業條碼了嗎?
IronBarcode讓條碼產生變得簡單,同時又能提供專業應用所需的彈性。 無論您需要簡單的產品代碼還是具有自訂樣式的複雜二維條碼, IronBarcode都能用最少的程式碼輕鬆處理。
立即下載IronBarcode ,幾分鐘內即可開始產生條碼。 需要協助選擇合適的許可證嗎? 查看我們的授權選項或申請免費試用金鑰,以便在您的生產環境中測試IronBarcode 。
常見問題解答
如何在 C# 中創建條碼圖片?
要在 C# 中創建條碼圖片,你可以使用 IronBarcode 的 BarcodeWriter.CreateBarcode() 方法。這允許您指定數據和條碼格式,然後使用 SaveAsPng() 等方法保存為 PNG 或 JPEG 格式的圖片。
在 .NET 項目中安裝 IronBarcode 的步驟有哪些?
您可以通過在 Visual Studio 中使用 NuGet 套件管理器在 .NET 項目中安裝 IronBarcode。或者,您可以從 IronBarcode 網站下載 DLL 並將其添加到項目引用中。
如何在 C# 中將條碼導出為 PDF?
IronBarcode 允許使用 SaveAsPdf() 方法從 GeneratedBarcode 類導出條碼為 PDF,提供了一種簡單的方法以 PDF 格式保存條碼。
在 C# 中條碼有哪些自定義選項?
IronBarcode 提供廣泛的自定義選項,例如使用 ChangeBarCodeColor() 更改條碼顏色,使用 AddAnnotationTextAboveBarcode() 添加文字註釋,以及使用 SetMargins() 設置邊距。
如何快速在一行代碼中創建和樣式化條碼?
使用 IronBarcode 的流暢 API,您可以通過方法鏈在一行中創建和樣式化條碼:BarcodeWriter.CreateBarcode(data, encoding).ResizeTo(300, 200).SetMargins(10).SaveAsPng(path)。
如何確保在修改後我的條碼可掃描?
在對條碼進行樣式或調整大小後,使用 GeneratedBarcode 對象的 Verify() 方法來驗證其機器可讀性。
我可以在 C# 中生成帶有標誌的 QR 碼嗎?
可以,IronBarcode 支持使用 QRCodeWriter 類生成帶有嵌入標誌的 QR 碼,其中包括標誌插入和增強的錯誤更正級別功能。
在 C# 中高效生成多個條碼的過程是什麼?
您可以使用 IronBarcode 高效生成多個條碼,這支持批量處理並允許使用循環或並行處理來處理高量條碼生成。
在 C# 中我可以使用什麼文件格式導出條碼?
IronBarcode 支持以多種格式導出條碼,包括 PNG、JPEG、GIF、TIFF、BMP、PDF 和 HTML,為不同的應用需求提供靈活性。
如何在 C# 中在條碼下添加人類可讀文本?
要在 C# 中在條碼下添加人類可讀文本,使用 AddBarcodeValueTextBelowBarcode() 方法,它會自動在條碼圖片下方顯示編碼值的文本格式。

