如何在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 應用程式中快速產生 Professional 級別的 BarCode 圖像嗎? 本教學將詳細示範如何使用 IronBarcode 建立、自訂及匯出 BARCODE——從簡單的一行程式碼實作,到能讓您完全掌控 BARCODE 外觀的高階樣式設定技巧。

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

透過 IronBarcode,您只需一次簡單的呼叫,即可生成並匯出 BarCode 圖像。 請在您的文字中使用 CreateBarcode 方法,選擇格式與大小,然後呼叫 SaveAsPng — 無需複雜的設定。

  1. using NuGet 套件管理員安裝 https://www.nuget.org/packages/BarCode

    PM > Install-Package BarCode
  2. 請複製並執行此程式碼片段。

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

    立即透過免費試用,在您的專案中開始使用 IronBarcode

    arrow pointer

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

using NuGet 套件管理員安裝 IronBarcode 只需幾秒鐘。 您可以透過套件管理主控台直接安裝,或手動下載 DLL 檔案。

Install-Package BarCode
IronBarcode 憑藉強大的功能與易於使用的 API,簡化了 .NET 應用程式中的 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})
$vbLabelText   $csharpLabel

BarcodeWriter.CreateBarcode() 方法是您生成 BARCODE 的入口點。 它接受兩個參數:您要編碼的資料,以及來自 BarcodeWriterEncoding 枚舉的 BARCODE 格式。 IronBarcode 支援所有主要 BarCode 格式,包括 DataMatrix 以及 QRCode 條碼。

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

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

GeneratedBarcode 類別提供了一套豐富的方法供使用者自訂:

  • 註解:請使用 AddAnnotationTextAboveBarcode()AddAnnotationTextBelowBarcode() 在BarCode周圍添加自訂標籤或說明
  • 值顯示AddBarcodeValueTextBelowBarcode() 方法會自動將編碼資料以人可讀的格式顯示
  • 間距:使用 SetMargins() 控制空白,以確保正確掃描及視覺美感
  • 顏色:使用 ChangeBarCodeColor()ChangeBackgroundColor() 變更前景色與背景色
  • 匯出選項:儲存為圖像檔案、PDF 或獨立的 HTML 文件
Customized purple QR code with annotations generated using IronBarcode styling features *一個採用自訂顏色並附有註解文字的樣式化 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
$vbLabelText   $csharpLabel

流暢 API 模式具備以下幾項優勢:

  • 可讀性:將操作以符合邏輯的順序串聯,使語意如自然語言般流暢
  • 效率:減少變數宣告與中間步驟
  • 靈活性:無需重構程式碼即可輕鬆新增或移除操作

常見的流暢操作包括:

  • ResizeTo():精確控制BARCODE尺寸
  • SetMargins(): 添加一致的空格
  • ChangeBarCodeColor(): 修改外觀
  • AddAnnotationTextAboveBarcode(): 包含說明性文字
  • ToBitmap(), SaveAsPng(), SaveAsPdf():匯出為多種格式
使用 IronBarcode 的流暢 API 並搭配自訂尺寸所建立的 PDF417 BarCode *A PDF417 barcode generated using fluent method chaining*

IronBarcode 支援哪些 BarCode 格式?

IronBarcode 透過 BarcodeWriterEncoding 枚舉支援全面的 BARCODE 格式生成。 支援的格式包括:

一維BARCODECode128, Code39, Code93, Codabar, ITF, MSI, Plessey, UPCA, UPCE, EAN8, EAN13
2D BARCODEMaxiCode
專用格式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"))}")
$vbLabelText   $csharpLabel

Verify() 方法用於檢查您的 BARCODE 在經過調整大小或重新上色等變換後,是否仍可被機器讀取。 當使用非標準顏色或極小字體時,這一點尤為重要。

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

若要擴展您的BarCode生成功能,請參考以下額外資源:

原始碼與範例

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

進階主題

API 文件

準備好在您的應用程式中生成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()方法,該方法會在條碼圖像下方自動顯示編碼值的文本格式。

雅各·梅勒(Jacob Mellor),Team Iron 首席技術長
技術長

雅各·梅勒(Jacob Mellor)是 Iron Software 的首席技術官,也是一位開創 C# PDF 技術的遠見卓識工程師。作為 Iron Software 核心程式碼庫的原始開發者,他自公司成立以來便塑造了產品架構,並與執行長卡梅隆·里明頓(Cameron Rimington)共同將公司發展為擁有 50 多名員工的企業,服務對象包括 NASA、特斯拉(Tesla)及全球政府機構。

雅各布於曼徹斯特大學(1998–2001)取得土木工程一等榮譽工程學士學位(BEng)。他在 1999 年於倫敦創立首家軟體公司,並於 2005 年開發出首批 .NET 元件,此後專注於解決微軟生態系統中的複雜問題。

其旗艦產品 IronPDF 與 Iron Suite .NET 函式庫在全球已累積超過 3,000 萬次 NuGet 安裝,其基礎程式碼持續驅動著全球廣泛使用的開發者工具。憑藉 25 年商業經驗與 41 年程式設計專業,雅各持續致力於推動企業級 C#、Java 及 Python PDF 技術的創新,同時指導新一代技術領導者。

準備好開始了嗎?
Nuget 下載 2,240,258 | 版本: 2026.5 just released
Still Scrolling Icon

還在捲動嗎?

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