如何在 C# .NET 應用程式中產生條碼圖像
需要在 .NET 應用程式中快速產生專業條碼圖像嗎? 本教學將向您展示如何使用 IronBarcode 建立、自訂和匯出條碼——從簡單的單行實現到進階樣式技術,讓您可以完全控制條碼的外觀。
快速入門:立即建立並儲存條碼圖像
使用 IronBarcode,只需一次簡單的呼叫即可產生和匯出條碼圖像。 使用 CreateBarcode 方法來處理您的文字,選擇格式和大小,然後呼喚 SaveAsPng - 不需要複雜的設定。
立即開始使用 NuGet 建立 PDF 檔案:
使用 NuGet 套件管理器安裝 IronBarcode
複製並運行這段程式碼。
IronBarCode.BarcodeWriter.CreateBarcode("Hello123", BarcodeWriterEncoding.Code128, 200, 100).SaveAsPng("barcode.png");部署到您的生產環境進行測試
最小工作流程(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 });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");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;流暢式 API 模式具有以下幾個優點:
-可讀性:將操作以邏輯順序串聯起來,使其讀起來像自然語言。 -效率:減少變數宣告與中間步驟 -靈活性:無需重構程式碼即可輕鬆新增或刪除操作
常見的流暢操作包括:
ResizeTo():精確控制條碼尺寸SetMargins():增加一致的間距ChangeBarCodeColor():修改外觀AddAnnotationTextAboveBarcode():新增描述性文字ToBitmap()、SaveAsPng()、SaveAsPdf():匯出為多種格式
A PDF417 barcode generated using fluent method chainingIronBarcode支援哪些條碼格式?
IronBarcode 透過BarcodeWriterEncoding枚舉支援全面的條碼格式產生。 支援的格式包括
1D BarCode:Code128, Code39, Code93, Codabar, ITF, MSI, Plessey, UPCA, UPCE, EAN8, EAN13 2D BarCode:QRCode、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")}");Verify()方法檢查條碼在套用調整大小或重新著色等轉換後是否仍可讀取機器。 當使用非標準顏色或非常小的尺寸時,這一點尤其重要。
哪裡可以找到更多條碼生成範例?
若要擴充條碼產生功能,請探索以下其他資源:
原始碼和範例
下載本教學的完整原始碼:
高級主題
- Generate QR Code with Logos - 為您的 QR 代碼加入品牌標誌
- BarCode Styling Guide - 掌握進階自訂技術
- 從影像讀取條碼 - 以條碼掃描完成循環 -批次產生條碼- 高效率產生多個條碼
API 文件
BarcodeWriter類別參考 - 完整的方法說明文件GeneratedBarcode類別參考 - 所有自訂選項BarcodeWriterEncoding枚舉- 支援的條碼格式
準備好在您的應用程式中產生專業條碼了嗎?
IronBarcode 讓條碼產生變得簡單,同時又能提供專業應用所需的彈性。 無論您需要簡單的產品程式碼還是具有自訂樣式的複雜二維條碼,IronBarcode 都能用最少的程式碼輕鬆處理。
立即下載 IronBarcode ,幾分鐘內即可開始產生條碼。 需要協助選擇合適的授權嗎? 查看我們的授權選項或申請免費試用金鑰,以便在您的生產環境中測試 IronBarcode。
常見問題解答
如何在C#中建立條碼影像?
若要在 C# 中建立條碼影像,可以使用 IronBarcode 的BarcodeWriter.CreateBarcode()方法。這允許您指定資料和條碼格式,然後使用SaveAsPng()等方法將影像儲存為 PNG 或 JPEG 等格式。
如何在.NET專案中安裝IronBarcode?
您可以使用 Visual Studio 中的 NuGet 套件管理器將 IronBarcode 安裝到您的 .NET 專案中。或者,您可以從 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 的二維碼嗎?
是的,IronBarcode 支援使用QRCodeWriter類別產生帶有嵌入式徽標的二維碼,該類別包含徽標插入功能和增強的錯誤修正等級。
如何在 C# 中高效率地產生多個條碼?
您可以使用 IronBarcode 在 C# 中有效地產生多個條碼,它支援批次處理,並允許使用循環或平行處理來處理大批量條碼產生。
在 C# 中,我可以使用哪些文件格式來匯出條碼?
IronBarcode 支援以 PNG、JPEG、GIF、TIFF、BMP、PDF 和 HTML 等多種格式匯出條碼,為不同的應用需求提供彈性。
如何在 C# 中為條碼下方添加易於閱讀的文字?
要在 C# 中條碼下方添加人類可讀的文本,請使用AddBarcodeValueTextBelowBarcode()方法,該方法會自動將編碼值以文本格式顯示在條碼圖像下方。






