如何在C#中建立Code 39條碼生成器
條碼已成為現代商業運營的重要組成部分,促進了高效的庫存管理、銷售點交易和資料跟蹤。 在各種條碼標準中,Code 39 脫穎而出,成為最廣泛使用和功能多樣的選擇之一。
校驗位,也稱為校驗碼或驗證碼,是新增到數字(或字母數字字元)序列中的一位,以幫助檢測資料中的錯誤。 校驗位的目的是透過提供一種簡單的錯誤檢測方法,來確保資料在傳輸或處理過程中的完整性。 校驗位的一個常見應用是在條碼中,通常用來驗證掃描資料的準確性。 使用校驗位的條碼標準之一是 Code 39。
Code 39 編碼字母數字字元,包括大寫字母、數字和一些特殊字元。 它包括一個起始字元、一個可選校驗碼字元和一個結束字元,使其具有自檢校驗以確保準確的資料捕捉。 此外,人類可讀的文字可以顯示在生成的條碼圖像下方。
IronBarcode(由Iron Software製作)是領先的 .NET C# 條碼程式庫,用於讀取和建立條碼。 使用者友好的 API 允許開發者在幾分鐘內將條碼功能新增到 .NET 應用程式中。 開發者可以使用此程式庫在幾分鐘內生成一個 Code 39 條碼專案和條碼測試。
在本文中,我們將探討使用 IronBarcode 構建 Code 39 條碼生成器的過程。
How to Create a Code 39 Barcode Generator in C
- 在 Visual Studio 中建立一個新的 C# 項目
- 安裝IronBarcode程式庫,並將其新增到您的項目中。
- 使用 IronBarcode 類程式庫生成 Code 39 條碼
- 向 Code 39 條碼圖像新增註釋文字
- 為 Code 39 條碼圖像新增樣式
必要條件
- Visual Studio:確保您已安裝 Visual Studio 或其他任何 C# 開發環境。
- NuGet Package Manager:確保您可以使用 NuGet 在項目中管理程式包。
Step 1: Create a New C# Project in Visual Studio
建立一個新的 C# 管理台應用程式,或使用您想要在其中生成新條碼圖像的現有項目。 此程式庫也可以在 .NET Windows Forms 應用程式上使用。 為了本教程的目的,我們將考慮一個控制臺應用程式。
選擇控制臺應用程式模板,然後點擊下一步。

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

選擇 .NET 版本並點擊"建立"。

步驟 2:安裝 IronBarcode 程式庫
IronBarcode can be installed from the NuGet Package Manager.

它也可以從 Visual Studio Package Manager 安裝。 在程式包管理器中搜尋 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")
這個簡單的程式初始化一個 BarcodeWriter C# 類,將編碼格式設置為 Code39,並使用提供的資料生成一個條碼 PNG。 然後條碼圖像被保存為 ironSoftwareBarcode.png。
輸出:

這裡我們使用 IronBarcode 程式庫中的 BarcodeWriter 類,根據提供的 URL 資料建立 Code 39 條碼。 每次運行程式碼時,會生成一個新的條碼圖像。
向 Code 39 條碼圖像新增註釋文字
using 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")
輸出:

在這裡您可以看到產品 URL 人類可讀文字被新增到條碼之上,條碼的人類可讀文字值被新增到條碼圖像之下。
向 Code 39 條碼圖像新增樣式
IronBarcode 允許條碼及其人類可讀文字被設置為樣式。 通常,設置條碼樣式包括調整大小、設置邊距、更改背景顏色、條碼顏色、字體以及驗證輸出的條碼仍可讀取。 所有這些方法都可以在 BarcodeWriter 物件上使用。 寬度和高度以像素設置。

使用流物件生成
該 BarcodeWriter 物件也可以像下面展示的那樣和 Stream 物件一起使用。 這在 Web API 應用程式中保存記憶體特別有幫助。 圖形物件也可以利用這一點。

授權(提供免費試用)
IronBarcode需要授權金鑰。 該金鑰需要放置在 appsettings.json。
{
"IronBarCode.LicenseKey": "MYLICENSE.KEY.TRIAL"
}
提供您的電子郵件以取得試用授權。 提交您的電子郵件 ID 後,該金鑰將通過電子郵件送達。

結論
在這本全面的指南中,我們探討了使用 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套件管理器安裝IronBarcode。使用BarcodeWriter類來生成和保存條碼影像。
安裝IronBarcode到C#專案的步驟是什麼?
要在C#專案中安裝IronBarcode,請打開Visual Studio中的NuGet套件管理器,搜尋‘IronBarcode’,然後點擊‘安裝’。這將把必要的程式庫新增到您的專案中,使條碼生成功能可用。
如何增強應用程式中的Code 39條碼的可見性?
IronBarcode允許您通過使用像AddAnnotationTextAboveBarcode和AddBarcodeValueTextBelowBarcode這樣的方法來新增註釋文字增強Code 39條碼的可見性,還可以自定顏色、字體和尺寸。
是否可以在網頁應用程式中高效生成Code 39條碼?
可以,通過使用IronBarcode和Stream物件,可以在網頁應用程式中高效生成Code 39條碼,節省記憶體並無縫整合到圖形物件中。
使用IronBarcode有哪些授權選項?
IronBarcode需要授權以實現全部功能。開發者可以通過提供電子郵件地址獲取試用授權,並通過電子郵件接收授權金鑰以供評估使用。
校驗碼如何增強條碼的完整性?
校驗碼是像Code 39這樣的條碼中的一個可選功能,通過在傳輸或處理過程中檢測錯誤來驗證資料的準確性,確保掃描資料的完整性。
使用IronBarcode在.NET應用程式中的優勢是什麼?
IronBarcode提供了快速整合條碼功能到.NET應用程式中的選項,支持各種自定義選項和高效的記憶體使用,使其成為開發者的寶貴工具。




