How to Generate Barcode Images in C# .NET Applications
需要在您的 .NET 應用程式中快速產生 Professional 級別的 BarCode 圖像嗎? 本教學將詳細示範如何使用 IronBarcode 建立、自訂及匯出 BARCODE——從簡單的一行程式碼實作,到能讓您完全掌控 BARCODE 外觀的高階樣式設定技巧。
快速入門:立即建立並儲存BarCode影像
透過 IronBarcode,您只需一次簡單的呼叫,即可生成並匯出 BarCode 圖像。 請在您的文字中使用 CreateBarcode 方法,選擇格式與大小,然後呼叫 SaveAsPng — 無需複雜的設定。
簡化工作流程(5 個步驟)
- 透過 NuGet 套件管理員安裝 IronBarcode
- 僅需一行程式碼即可生成簡易BarCode
- 為您的BarCode套用自訂樣式與註解
- 將 BarCode 匯出為圖片、PDF 或 HTML
- 使用 Fluent API 高效生成 BarCode
How Do I Install a Barcode Generator Library in C#?
using NuGet 套件管理員安裝 IronBarcode 只需幾秒鐘。 您可以透過套件管理主控台直接安裝,或手動下載 DLL 檔案。
Install-Package BarCode
IronBarcode for .NET 為 .NET 開發人員提供全面的 BarCode 生成功能
How Can I Generate a Simple Barcode Using C#?
只需兩行程式碼,即可建立您的第一個BARCODE。 以下範例展示如何生成標準的 Code128 BARCODE,並將其儲存為圖像檔案。
using IronBarCode;
// Create a barcode with your desired content and encoding type
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.Co/de128);
// 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.Co/de128);
// 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
Imports System.Diagnostics
' Create a barcode with your desired content and encoding type
Dim 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
Process.Start(New ProcessStartInfo("myBarcode.png") With {.UseShellExecute = True})
BarcodeWriter.CreateBarcode() 方法是您生成 BARCODE 的入口點。 它接受兩個參數:您要編碼的資料,以及來自 BarcodeWriterEncoding 枚舉的 BARCODE 格式。 IronBarcode 支援所有主要 BarCode 格式,包括 DataMatrix 以及 QRCode 條碼。
一旦產生,GeneratedBarcode 物件便提供多種匯出選項。 您可以將其儲存為各種圖像格式(PNG、JPEG、GIF、TIFF),匯出為 PDF,甚至以 System.Drawing.Bitmap 格式擷取,以便在您的應用程式中進行進一步處理。
*A Code128 barcode generated with IronBarcode displaying a URL*
我可以自訂生成的BarCode外觀嗎?
IronBarcode 提供廣泛的自訂選項,其功能遠超出基本的 BarCode 生成範疇。 您可以添加註解、調整顏色、設定邊距,並控制BARCODE外觀的每個細節。
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()在BarCode周圍添加自訂標籤或說明 - 值顯示:
AddBarcodeValueTextBelowBarcode()方法會自動將編碼資料以人可讀的格式顯示 - 間距:使用
SetMargins()控制空白,以確保正確掃描及視覺美感 - 顏色:使用
ChangeBarCodeColor()和ChangeBackgroundColor()變更前景色與背景色 - 匯出選項:儲存為圖像檔案、PDF 或獨立的 HTML 文件
*一個採用自訂顏色並附有註解文字的樣式化 QR 碼*如需詳細的自訂選項,請參閱 GeneratedBarcode 類別文件,其中涵蓋所有可用的樣式設定方法與屬性。
如何用一行程式碼建立並匯出BarCode?
IronBarcode 採用流暢 API 設計模式,透過方法鏈接實現更簡潔且易於閱讀的程式碼。 此方法在對BARCODE進行多重轉換時特別有用。
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():精確控制BARCODE尺寸SetMargins(): 添加一致的空格ChangeBarCodeColor(): 修改外觀AddAnnotationTextAboveBarcode(): 包含說明性文字ToBitmap(),SaveAsPng(),SaveAsPdf():匯出為多種格式
*A PDF417 barcode generated using fluent method chaining*
IronBarcode 支援哪些 BarCode 格式?
IronBarcode 透過 BarcodeWriterEncoding 枚舉支援全面的 BARCODE 格式生成。 支援的格式包括:
一維BARCODE:Code128, Code39, Code93, Codabar, ITF, MSI, Plessey, UPCA, UPCE, EAN8, EAN13
2D BARCODE:MaxiCode
專用格式:DataBarExpanded 以及各種 GS1 標準
每種格式皆具備特定的特性與應用情境。 例如,QRCode 擅長儲存 URL 和大量資料,而 EAN13 則是零售產品的標準選擇。 進一步了解如何為您的應用程式選擇合適的BarCode格式。
如何驗證生成的BarCode是否可讀?
品質保證對於BARCODE的實作至關重要。 IronBarcode 內建驗證功能,以確保您生成的 BarCode 始終可被掃描:
// Generate and verify a barcode
GeneratedBarcode myBarcode = BarcodeWriter
.CreateBarcode("TEST123", BarcodeWriterEncoding.Co/de128)
.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.Co/de128)
.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")}");
Imports System.Drawing
' 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() 方法用於檢查您的 BARCODE 在經過調整大小或重新上色等變換後,是否仍可被機器讀取。 當使用非標準顏色或極小字體時,這一點尤為重要。
哪裡可以找到更多BarCode生成範例?
若要擴展您的BarCode生成功能,請參考以下額外資源:
原始碼與範例
下載本教學的完整原始碼:
進階主題
- 生成帶有標誌的 QR 碼 - 為您的 QR 碼添加品牌標識
- BarCode樣式指南 - 掌握進階自訂技巧
- 從圖片讀取BarCode - 透過BarCode掃描完成整個流程
- 批次BarCode生成 - 高效生成多個BarCode
API 文件
BarcodeWriter類別參考 - 完整的方法文件GeneratedBarcode類別參考 - 所有自訂選項BarcodeWriterEncodingEnum - 支援的 BARCODE 格式
準備好在您的應用程式中生成Professional BARCODEs了嗎?
IronBarcode 讓 BarCode 生成變得簡單直觀,同時提供 Professional 應用所需的靈活性。 無論您需要簡單的產品代碼,還是具有自訂樣式的複雜 2D BARCODE,IronBarcode 都能以最少的程式碼輕鬆處理。
立即下載 IronBarcode,幾分鐘內即可開始生成 BARCODE。 需要協助選擇合適的授權嗎? 請查看我們的授權方案,或申請免費試用授權,在您的生產環境中測試 IronBarcode。
常見問題
如何在C#中創建條碼圖像?
要在C#中創建條碼圖像,您可以使用IronBarcode的BarcodeWriter.CreateBarcode()方法。這允許您指定數據和條碼格式,然後使用如SaveAsPng()的方法以PNG或JPEG格式保存圖像。
在.NET專案中安裝IronBarcode的步驟是什麼?
您可以在Visual Studio中使用NuGet套件管理器安裝IronBarcode。或者,您可以從IronBarcode網站下下載DLL並添加到您的專案引用中。
如何在C#中將條碼匯出為PDF?
IronBarcode允許使用GeneratedBarcode類的SaveAsPdf()方法將條碼匯出為PDF,提供了一種直接將條碼保存為PDF格式的方法。
在C#中條碼有哪些自訂選項?
IronBarcode提供了廣泛的自訂選項,例如使用ChangeBarCodeColor()更改條碼顏色,使用AddAnnotationTextAboveBarcode()添加文字註解,和使用SetMargins()設置邊距。
如何在一行代碼中快速創建和樣式化條碼?
使用IronBarcode的Fluent API,您可以通過方法鏈在一行中創建和樣式化條碼:BarcodeWriter.CreateBarcode(data, encoding).ResizeTo(300, 200).SetMargins(10).SaveAsPng(path)。
如何確保條碼在修改後依然可掃描?
在樣式化或調整大小後,您可以在GeneratedBarcode對象上使用Verify()方法來檢查其機器讀取性。
我可以使用C#生成帶Logo的QR碼嗎?
可以,IronBarcode支持使用QRCodeWriter類生成帶嵌入Logo的QR碼,該類包含Logo插入和增強錯誤修正等功能。
如何在C#中有效地生成多個條碼?
您可以使用IronBarcode在C#中有效地生成多個條碼,該工具支持批處理,允許使用迴圈或並行處理來處理高容量條碼生成。
我可以使用哪些文件格式在C#中匯出條碼?
IronBarcode支持以各種格式匯出條碼,包括PNG、JPEG、GIF、TIFF、BMP、PDF、和HTML,以滿足不同應用需求。
如何在C#中在條碼下添加人類可讀的文字?
要在C#中在條碼下方添加人類可讀的文字,使用AddBarcodeValueTextBelowBarcode()方法,該方法會在條碼圖像下方自動顯示編碼值的文本格式。

