如何在 C# 中生成條碼圖片

How to Generate Barcode Images in C# .NET Applications

This article was translated from English: Does it need improvement?
Translated
View the article in English

需要在.NET應用程式中快速產生專業條碼圖像嗎? 本教學將向您展示如何使用IronBarcode來建立、自訂和匯出條碼——從簡單的單行實現到進階樣式技術,讓您可以完全控制條碼的外觀。

快速入門:立即建立並儲存條碼影像快速入門:立即建立並儲存條碼影像

使用IronBarcode ,只需一次簡單的呼叫即可產生和匯出條碼影像。 使用 CreateBarcode 方法輸入文本,選擇格式和大小,然後呼叫 SaveAsPng — 無需複雜的設定。

  1. 使用NuGet套件管理器安裝https://www.nuget.org/packages/BarCode

    PM > Install-Package BarCode
  2. 複製並運行這段程式碼。

    IronBarCode.BarcodeWriter.CreateBarcode("Hello123", BarcodeWriterEncoding.Code128, 200, 100).SaveAsPng("barcode.png");
  3. 部署到您的生產環境進行測試

    今天就在您的專案中開始使用免費試用IronBarcode

    arrow pointer

How Do I Install a Barcode Generator Library in C#?

使用NuGet套件管理器安裝IronBarcode只需幾秒鐘。 您可以直接透過套件管理器控制台安裝,也可以手動下載 DLL 檔案。

Install-Package BarCode
IronBarcode 透過強大的功能和易於使用的 API 簡化了 .NET 應用程式中的條碼產生。 *IronBarcode為.NET開發人員提供全面的條碼產生功能。*

How Can I Generate a Simple Barcode Using 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 });
$vbLabelText   $csharpLabel

BarcodeWriter.CreateBarcode() 方法是您產生條碼的入口點。 它接受兩個參數:要編碼的資料和來自 BarcodeWriterEncoding 枚舉的條碼格式。 IronBarcode支援所有主流條碼格式,包括 Code128、Code39、EAN13、UPC-A、PDF417、DataMatrix 和 QR 碼。

生成後,GeneratedBarcode 物件提供多種匯出選項。 您可以將其儲存為各種影像格式(PNG、JPEG、GIF、TIFF),匯出為 PDF,甚至可以將其擷取為 System.Drawing.Bitmap 以便在您的應用程式中進行進一步處理。

使用 C# 中的IronBarcode產生 Code128 條碼的範例 *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");
$vbLabelText   $csharpLabel

GeneratedBarcode 類別提供了一組豐富的自訂方法:

-:使用 AddAnnotationTextAboveBarcode()AddAnnotationTextBelowBarcode() 在條碼周圍新增自訂標籤或說明。 -值顯示AddBarcodeValueTextBelowBarcode() 方法會自動以人類可讀格式顯示編碼資料。 -間距:使用 SetMargins() 控制空白,以確保正確掃描和視覺效果。 -顏色:使用 ChangeBarCodeColor()ChangeBackgroundColor() 變更前景色和背景色 匯出選項:儲存為影像檔案、PDF 或獨立的 HTML 文件

Customized purple QR code with annotations generated using IronBarcode styling features *帶有自訂顏色和註釋文字的樣式化二維碼*

有關詳細的自訂選項,請瀏覽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;
$vbLabelText   $csharpLabel

流暢式 API 模式具有以下幾個優點:

-可讀性:將操作以邏輯順序串聯起來,使其讀起來像自然語言。 -效率:減少變數宣告與中間步驟 -靈活性:無需重構程式碼即可輕鬆新增或刪除操作

常見的流暢操作包括:

  • ResizeTo(): 精確控制條碼尺寸
  • SetMargins(): 新增一致間距
  • ChangeBarCodeColor(): 修改外觀
  • AddAnnotationTextAboveBarcode(): 包含描述性文本
  • SaveAsPdf():匯出為多種格式
使用 IronBarcode 的 Fluent API 建立的具有自訂尺寸的 PDF417 條碼 *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")}");
$vbLabelText   $csharpLabel

Verify() 方法檢查條碼在套用調整大小或重新著色等轉換後是否仍可讀取機器。 當使用非標準顏色或非常小的尺寸時,這一點尤其重要。

哪裡可以找到更多條碼生成範例?

若要擴充條碼產生功能,請探索以下其他資源:

原始碼和範例

下載本教學的完整原始碼:

高級主題

-產生帶有徽標的二維碼- 為您的二維碼添加品牌標識
條碼樣式指南- 掌握進階自訂技巧
-從影像中讀取條碼- 透過條碼掃描完成循環
-批次產生條碼- 高效率產生多個條碼

API 文件

準備好在您的應用程式中產生專業條碼了嗎?

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() 方法,它會自動在條碼圖片下方顯示編碼值的文本格式。

Jacob Mellor, Team Iron 首席技術官
首席技術官

Jacob Mellor是Iron Software的首席技術官,也是開創C# PDF技術的前瞻性工程師。作為Iron Software核心代碼庫的原始開發者,他自公司成立以來就塑造了公司的產品架構,並與CEO Cameron Rimington將公司轉型為服務NASA、Tesla以及全球政府機構的50多人公司。

Jacob擁有曼徹斯特大學土木工程一級榮譽學士學位(1998年–2001年)。他於1999年在倫敦開立首家軟體公司,並於2005年建立了他的第一個.NET組件,專注於解決Microsoft生態系統中的複雜問題。

他的旗艦作品IronPDF和Iron Suite .NET程式庫全球已獲得超過3000萬次NuGet安裝,他的基礎代碼不斷在全球各地驅動開發者工具。擁有25年以上的商業經驗和41年的編碼專業知識,Jacob仍然專注於推動企業級C#、Java和Python PDF技術的創新,同時指導下一代技術領導者。

準備好開始了嗎?
Nuget 下載 2,108,094 | 版本: 2026.3 剛剛發布
Still Scrolling Icon

還在捲動嗎?

想要快速證明? PM > Install-Package BarCode
執行範例 看您的字串變成 BarCode。