
條碼生成器.NET教程
由於條碼使用的迅速增長,開發者必須能夠在他們偏好的程式語言中生成條碼。 因此,本教學將展示如何在 .NET 中生成條碼。
條碼生成器 .NET 教學
- 在 Visual Studio 中建立專案
- 安裝 C# 條碼生成器程式庫
- 設計 Windows Forms 應用程式的使用者介面
- 編寫核心功能程式碼
- 執行 .NET 條碼生成器
讓我們開始教學。
建立專案
本教學使用 Visual Studio 的最新版本和 Windows Forms 應用程式模板。 您可以使用自己選擇的應用程式,並使用現有的專案和版本。
打開 Visual Studio > 點擊 建立新專案 > 選擇 Windows Forms 應用程式模板 > 按 下一步 > 為專案命名 > 按 下一步 => 選擇目標 .NET Framework => 點擊 建立 按鈕。
建立一個新的 Windows Forms 應用程式
安裝條碼程式庫
安裝條碼生成器程式庫有很多好處。 以 C# 編寫的 IronBarcode 提供了只需一行程式碼就能建立條碼和 QR 碼的功能。 它還支持將 QR 碼或條碼儲存為所需的文件格式。 此外,它還為 .NET 中的條碼生成提供免費服務和運行時支援。
讓我們從安裝 IronBarcode NuGet 套件開始。 您可以使用以下三種方法之一來安裝它:
套件管理器主控台
在套件管理器主控台中輸入以下命令。 它會為您下載並安裝該套件。
套件管理器主控台安裝步驟
NuGet 套件管理器解決方案
您還可以使用 NuGet 套件解決方案安裝條碼套件。 只需遵循以下步驟:
點擊 工具 > NuGet 套件管理器 > 為解決方案管理 NuGet 套件。
這將為您打開 NuGet 套件管理器。 點擊 Browse 並搜尋 "IronBarcode",然後安裝該程式庫。
NuGet 套件管理器介面
從連結下載
作為替代方案,IronBarCode.Dll 可以下載並作為 .NET 條碼 DLL 的引用新增到您的專案中。
設計 Windows Forms
.NET 條碼生成器的使用者介面應具有 2 個標籤、1 個富文字框和 1 個圖片框,以顯示生成的條碼圖像。 下圖顯示了一個用於演示的簡單設計。
設計 Windows Forms 應用程式
編寫條碼生成程式
雙擊 "生成" 按鈕。 以下程式碼將出現:
private void button1_Click(object sender, EventArgs e)
{
// This function will be triggered when the "Generate" button is clicked
}Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
' This function will be triggered when the "Generate" button is clicked
End Sub在程式碼文件的頂部新增以下命名空間:
using IronBarCode; // Import the IronBarCode library to handle barcode operations
using System.Drawing; // Import for image manipulation
using System.Windows.Forms; // Import for Windows Forms functionalityImports IronBarCode ' Import the IronBarCode library to handle barcode operations
Imports System.Drawing ' Import for image manipulation
Imports System.Windows.Forms ' Import for Windows Forms functionality在 button1_Click() 函式中寫下以下程式碼:
// Generate a barcode with the specified value and encoding
GeneratedBarcode MyBarCode = IronBarCode.BarcodeWriter.CreateBarcode(BarcodeValue.Text, BarcodeWriterEncoding.Code128);
// Save the generated barcode as a PNG file
MyBarCode.SaveAsPng("MyBarCode.png");
// Display the generated barcode image in the PictureBox
BarcodeImage.Image = new Bitmap("MyBarCode.png");' Generate a barcode with the specified value and encoding
Dim MyBarCode As GeneratedBarcode = IronBarCode.BarcodeWriter.CreateBarcode(BarcodeValue.Text, BarcodeWriterEncoding.Code128)
' Save the generated barcode as a PNG file
MyBarCode.SaveAsPng("MyBarCode.png")
' Display the generated barcode image in the PictureBox
BarcodeImage.Image = New Bitmap("MyBarCode.png")讓我們逐行理解程式碼:
-
GeneratedBarcode是表示生成的條碼的資料型別。 -
CreateBarcode是BarcodeWriter類中的一個函式,位於IronBarCode套件中,用於根據使用者輸入生成條碼。 -
BarcodeValue.Text檢索使用者輸入的文字,這些文字將被編碼到條碼中。 -
BarcodeWriterEncoding.Code128指定生成條碼的編碼方案。 您可以將其更改為其他編碼型別,如BarcodeWriterEncoding.QRCode以生成 QR 碼。 -
SaveAsPng("MyBarCode.png")將條碼圖像保存為 PNG 文件。 -
BarcodeImage是表單上的 PictureBox 控件,用於向使用者顯示條碼圖像。
運行 .NET 條碼生成器
按 Ctrl + F5 運行應用程式。
運行條碼生成器應用程式
在文字框中輸入您想要編碼到條碼中的值,如下所示。
粘貼 URL 以生成條碼
現在,點擊 "生成" 按鈕。 條碼將如下面所示生成。
在 Windows Forms 應用程式中生成的條碼
顯示條碼的值
接下來,您可以用一行程式碼顯示條碼的值:
// Add the encoded barcode value as text below the barcode image
MyBarCode.AddBarcodeValueTextBelowBarcode();' Add the encoded barcode value as text below the barcode image
MyBarCode.AddBarcodeValueTextBelowBarcode()輸出
從字串值生成條碼
總結
IronBarcode 為開發者提供了一個友好的 API,允許在 .NET 中讀取和寫入條碼,優化準確性並在現實軟體中確保低錯誤率。 造訪官方文件頁面以獲取有關 IronBarcode 的更多資訊。
目前,如果您購買完整的 Iron Suite,您可以以兩個程式庫的價格獲得五個程式庫。 如需更多資訊。

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。
相關文章


