跳過到頁腳內容
使用IRONBARCODE

如何在C#中創建Code 39條碼生成器

條碼已成為現代商業運作中不可或缺的一部分,有助於實現高效的庫存管理、銷售點交易和數據追踪。 在各種條碼符號中,Code 39 脫穎而出,成為最廣泛使用且用途多樣的選擇之一。

校驗碼,又稱為檢查碼或驗證碼,是添加到數字序列(或字母數字字符)的數位,以幫助檢測數據中的錯誤。 校驗碼的目的是通過提供一種簡單的錯誤檢測方法來確保數據的完整性,無論是在數據傳輸還是處理過程中。 校驗碼的一個常見應用是在條碼中,通常用於驗證掃描數據的準確性。 使用校驗碼的條碼符號之一是 Code 39。

Code 39 編碼字母數字字符,包括大寫字母、數字和少數特殊字符。 它包括一個起始字符、一個可選的校驗碼字符和一個終止字符,使其具有自檢功能以確保準確的數據捕獲。 此外,可以在生成的條碼圖像下方顯示人類可讀文本。

IronBarcode (produced by Iron Software 製作)是領先的 .NET C# 條碼庫,用於讀取和創建條碼。 用戶友好的 API 允許開發人員在幾分鐘內將條碼功能添加到 .NET 應用程序中。 開發人員可以使用此庫在幾分鐘內生成 Code 39 條碼項目和條碼測試。

在本文中,我們將探討使用 IronBarcode 構建 Code 39 條碼生成器的過程。

如何在 C&# 中創建 Code 39 條碼生成器

  1. 在 Visual Studio 中創建一個新的 C# 項目
  2. 安裝 IronBarcode 庫並將其添加到您的項目中。
  3. 使用 IronBarcode 類庫生成 Code 39 條碼
  4. 為 Code 39 條碼圖像添加註解文本
  5. 為 Code 39 條碼圖像添加樣式

必要條件

  1. Visual Studio:確保您已安裝 Visual Studio 或任何其他 C# 開發環境。
  2. NuGet 包管理器:確保可以使用 NuGet 管理項目中的包。

步驟 1:在 Visual Studio 中創建一個新的 C&# 項目

創建一個新的 C# 控制台應用程序或使用現有項目,在其中生成新的條碼圖像。 此庫也可用於 .NET Windows 窗體應用程序。 就本教程而言,我們考慮使用控制台應用程序。

選擇控制台應用程序模板,然後點擊下一步。

如何在 C# 中創建 Code 39 條碼生成器:圖 1 - 選擇控制台應用程序模板

在下一步中,您可以提供解決方案和項目名稱。

如何在 C# 中創建 Code 39 條碼生成器:圖 2 - 配置項目名稱和解決方案

選擇 .NET 版本並點擊“創建”。

如何在 C# 中創建 Code 39 條碼生成器:圖 3 - 使用正確的 .NET 版本創建項目

步驟 2:安裝 IronBarcode 庫

IronBarcode can be installed from the NuGet 包管理器安裝。

如何在 C# 中創建 Code 39 條碼生成器:圖 4 - 在 NuGet 包管理器上的 IronBarcode

它也可以從 Visual Studio 包管理器中安裝。 在包管理器中搜索 IronBarcode,然後點擊安裝。

如何在 C# 中創建 Code 39 條碼生成器:圖 5 - 從 Visual Studio 包管理器中安裝 IronBarcode

步驟 3:使用 IronBarcode 庫生成 Code 39 條碼

現在,讓我們編寫代碼以使用 IronBarcode 庫生成 Code 39 條碼。 下面是一個簡單的例子:

using IronBarCode;

Console.WriteLine("Code 39 Barcode Generator");

// Generate a Code 39 Barcode using the BarcodeWriter class
GeneratedBarcode code39Barcode = BarcodeWriter.CreateBarcode(
    "https://ironsoftware.com/csharp/barcode", 
    BarcodeEncoding.Code39
);

// Save the generated barcode image as a PNG file
code39Barcode.SaveAsImage("ironSoftwareBarcode.png");
using IronBarCode;

Console.WriteLine("Code 39 Barcode Generator");

// Generate a Code 39 Barcode using the BarcodeWriter class
GeneratedBarcode code39Barcode = BarcodeWriter.CreateBarcode(
    "https://ironsoftware.com/csharp/barcode", 
    BarcodeEncoding.Code39
);

// Save the generated barcode image as a PNG file
code39Barcode.SaveAsImage("ironSoftwareBarcode.png");
Imports IronBarCode

Console.WriteLine("Code 39 Barcode Generator")

' Generate a Code 39 Barcode using the BarcodeWriter class
Dim code39Barcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code39)

' Save the generated barcode image as a PNG file
code39Barcode.SaveAsImage("ironSoftwareBarcode.png")
$vbLabelText   $csharpLabel

此簡單程序初始化了一個 BarcodeWriter C# 類,將編碼格式設置為 Code39,並使用提供的數據生成條碼 PNG。 然後將條碼圖像保存為 ironSoftwareBarcode.png

輸出:

如何在 C# 中創建 Code 39 條碼生成器:圖 6 - 輸出的條碼圖像以 Code 39 編碼

在此,我們使用 IronBarcode 庫中的 BarcodeWriter 類來創建具有提供的 URL 數據的 Code 39 條碼。 每次運行代碼時,都會生成一個新的條碼圖像。

為 Code 39 條碼圖像添加註解文本

可以使用 IronBarcode 輕鬆地將註解文本添加到條碼中。 BarcodeWriter 對象生成了一個 Fluent API 的條碼對象,允許在一行代碼中設置條碼文本。

using IronBarCode;

Console.WriteLine("Code 39 Barcode Generator");

// Generate a Code 39 Barcode
GeneratedBarcode code39Barcode = BarcodeWriter.CreateBarcode(
    "https://ironsoftware.com/csharp/barcode", 
    BarcodeEncoding.Code39
);

// Add annotation text above and the barcode value text below the barcode
code39Barcode.AddAnnotationTextAboveBarcode("Product URL:");
code39Barcode.AddBarcodeValueTextBelowBarcode();

// Save the barcode image with annotation text
code39Barcode.SaveAsImage("ironSoftwareBarcodeWithText.png");
using IronBarCode;

Console.WriteLine("Code 39 Barcode Generator");

// Generate a Code 39 Barcode
GeneratedBarcode code39Barcode = BarcodeWriter.CreateBarcode(
    "https://ironsoftware.com/csharp/barcode", 
    BarcodeEncoding.Code39
);

// Add annotation text above and the barcode value text below the barcode
code39Barcode.AddAnnotationTextAboveBarcode("Product URL:");
code39Barcode.AddBarcodeValueTextBelowBarcode();

// Save the barcode image with annotation text
code39Barcode.SaveAsImage("ironSoftwareBarcodeWithText.png");
Imports IronBarCode

Console.WriteLine("Code 39 Barcode Generator")

' Generate a Code 39 Barcode
Dim code39Barcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code39)

' Add annotation text above and the barcode value text below the barcode
code39Barcode.AddAnnotationTextAboveBarcode("Product URL:")
code39Barcode.AddBarcodeValueTextBelowBarcode()

' Save the barcode image with annotation text
code39Barcode.SaveAsImage("ironSoftwareBarcodeWithText.png")
$vbLabelText   $csharpLabel

輸出:

如何在 C# 中創建 Code 39 條碼生成器:圖 7 - 從先前代碼輸出的帶註解條碼圖像

在這裡,您可以看到產品 URL 的人類可讀文本被添加到了條碼上方,且條碼的人類可讀文本值被添加至條碼圖像下方。

為 Code 39 條碼圖像添加樣式

IronBarcode 允許對條碼和人類可讀文本進行樣式設置。 通常,對條碼進行樣式設置包括調整大小、設置邊距、更改背景顏色、更改條碼顏色、字體,以及驗證輸出條碼仍可讀。 所有這些方法均可在 BarcodeWriter 對象上使用。 寬度和高度以像素為單位設置。

如何在 C# 中創建 Code 39 條碼生成器:圖 8 - 如何簡單地為 BarcodeWriter 對象添加樣式

使用流對象生成

BarcodeWriter 對象還可以與流對象一起工作,如下所示。 這在 Web API 應用程序中特別有助於節省內存。 圖形對象也可以利用這一點。

如何在 C# 中創建 Code 39 條碼生成器:圖 9 - 使用流類型對象生成條碼

許可(可用免費試用版)

IronBarcode 需要許可證密鑰。 密鑰需要放在 appsettings.json 中。

{
    "IronBarcode.LicenseKey": "MYLICENSE.KEY.TRIAL"
}

提供您的電子郵件以獲取試用許可。 在提交您的電子郵件 ID 後,密鑰將通過電子郵件發送。

如何在 C# 中創建 Code 39 條碼生成器:圖 10 - 彈出顯示成功申請試用許可

結論

在這份全面指南中,我們探討了使用 C# 編程語言構建 Code 39 條碼生成器的過程。 Code 39 是一種用途廣泛且廣泛使用的條碼符號,以其簡易性和可編碼字母數字字符而聞名。 通過利用 IronBarcode 庫的功能,我們展示了一個逐步的方法來創建一個 C# 應用程序,該應用程序能生成具有可選校驗碼的 Code 39 條碼。

隨著技術的持續發展,準確高效的數據編/解碼的重要性變得愈加關鍵。 在 C# 中構建 Code 39 條碼生成器不僅為企業和開發人員提供了一種實用工具,還作為理解條碼符號、校驗碼算法及 C# 應用中第三方庫集成的教育練習。

總而言之,這本指南為開發人員提供了創建強大 Code 39 條碼生成器所需的知識和工具,促進其項目中可靠的條碼解決方案集成。 無論您是經驗豐富的開發人員還是條碼生成的新手,本文提供了進一步探索和基於您應用程序特定需求進行定制的堅實基礎。

常見問題解答

什麼是 Code 39,為何它很受歡迎?

Code 39 是一種可編碼字母數字字符的條碼符號學,包括大寫字母、數字和一些特殊字符。由於其簡單性和多功能性,它受到歡迎,適合各種商業操作應用程式。

如何在 C# 中創建一個 Code 39 條碼生成器?

您可以使用 IronBarcode 在 C# 中創建 Code 39 條碼生成器。首先,在 Visual Studio 中設置您的 C# 項目,然後通過 NuGet Package Manager 安裝 IronBarcode。使用 BarcodeWriter 類生成和保存條碼圖像。

安裝 IronBarcode 到 C# 項目的步驟是什麼?

要在 C# 項目中安裝 IronBarcode,打開 Visual Studio 中的 NuGet Package Manager,搜索 'IronBarcode',然後點擊 '安裝'。這將把必要的庫添加到您的項目中,啟用條碼生成。

如何在我的應用程式中增強 Code 39 條碼的可見性?

IronBarcode 允許您通過添加註解文字來增強 Code 39 條碼的可見性,使用方法如 AddAnnotationTextAboveBarcodeAddBarcodeValueTextBelowBarcode,還有自定義顏色、字體和大小。

可以在網絡應用程式中高效地生成 Code 39 條碼嗎?

是的,通過使用 IronBarcode 和 Stream 對象,您可以在網絡應用程式中高效地生成 Code 39 條碼,節省內存並與圖形對象無縫集成。

IronBarcode 可用的許可選項有哪些?

IronBarcode 需要許可才能充分發揮功能。開發者可以通過提供他們的電子郵件地址獲取試用許可,在電子郵件中接收許可密鑰以供評估使用。

校驗和如何增強條碼的完整性?

校驗和是像 Code 39 這樣的條碼中一個可選功能,它通過檢測傳輸或處理過程中的錯誤來驗證數據的準確性,以確保掃描數據的完整性。

使用 IronBarcode 於 .NET 應用程式的好處是什麼?

IronBarcode 提供快速整合條碼功能到 .NET 應用程式,支持各種定制選項和高效的內存使用,使其成為開發者的寶貴工具。

Jordi Bardia
軟體工程師
Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担产品测测试,产品开发和研究的责任时,Jordi 为持续的产品改进增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。