跳至頁尾內容
使用IRONBARCODE

條碼生成器.NET教程

由於條碼使用的迅速增長,開發者必須能夠在他們偏好的程式語言中生成條碼。 因此,本教學將展示如何在 .NET 中生成條碼。

條碼生成器 .NET 教學

  1. 在 Visual Studio 中建立專案
  2. 安裝 C# 條碼生成器程式庫
  3. 設計 Windows Forms 應用程式的使用者介面
  4. 編寫核心功能程式碼
  5. 執行 .NET 條碼生成器

讓我們開始教學。

建立專案

本教學使用 Visual Studio 的最新版本和 Windows Forms 應用程式模板。 您可以使用自己選擇的應用程式,並使用現有的專案和版本。

打開 Visual Studio > 點擊 建立新專案 > 選擇 Windows Forms 應用程式模板 > 按 下一步 > 為專案命名 > 按 下一步 => 選擇目標 .NET Framework => 點擊 建立 按鈕。

條碼生成器 .NET 教學,圖 1:建立一個新的 Windows Forms 應用程式 建立一個新的 Windows Forms 應用程式

安裝條碼程式庫

安裝條碼生成器程式庫有很多好處。 以 C# 編寫的 IronBarcode 提供了只需一行程式碼就能建立條碼和 QR 碼的功能。 它還支持將 QR 碼或條碼儲存為所需的文件格式。 此外,它還為 .NET 中的條碼生成提供免費服務和運行時支援。

讓我們從安裝 IronBarcode NuGet 套件開始。 您可以使用以下三種方法之一來安裝它:

套件管理器主控台

在套件管理器主控台中輸入以下命令。 它會為您下載並安裝該套件。

Install-Package BarCode

條碼生成器 .NET 教學,圖 2:套件管理器主控台安裝步驟 套件管理器主控台安裝步驟

NuGet 套件管理器解決方案

您還可以使用 NuGet 套件解決方案安裝條碼套件。 只需遵循以下步驟:

點擊 工具 > NuGet 套件管理器 > 為解決方案管理 NuGet 套件

這將為您打開 NuGet 套件管理器。 點擊 Browse 並搜尋 "IronBarcode",然後安裝該程式庫。

條碼生成器 .NET 教學,圖 3:NuGet 套件管理器介面 NuGet 套件管理器介面

從連結下載

作為替代方案,IronBarCode.Dll 可以下載並作為 .NET 條碼 DLL 的引用新增到您的專案中。

設計 Windows Forms

.NET 條碼生成器的使用者介面應具有 2 個標籤、1 個富文字框和 1 個圖片框,以顯示生成的條碼圖像。 下圖顯示了一個用於演示的簡單設計。

條碼生成器 .NET 教學,圖 4:設計 Windows Forms 應用程式 設計 Windows Forms 應用程式

編寫條碼生成程式

雙擊 "生成" 按鈕。 以下程式碼將出現:

private void button1_Click(object sender, EventArgs e)
{
    // This function will be triggered when the "Generate" button is clicked
}
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
$vbLabelText   $csharpLabel

在程式碼文件的頂部新增以下命名空間:

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 functionality
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 functionality
Imports 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
$vbLabelText   $csharpLabel

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
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")
$vbLabelText   $csharpLabel

讓我們逐行理解程式碼:

  • GeneratedBarcode 是表示生成的條碼的資料型別。

  • CreateBarcodeBarcodeWriter 類中的一個函式,位於 IronBarCode 套件中,用於根據使用者輸入生成條碼。

  • BarcodeValue.Text 檢索使用者輸入的文字,這些文字將被編碼到條碼中。

  • BarcodeWriterEncoding.Code128 指定生成條碼的編碼方案。 您可以將其更改為其他編碼型別,如 BarcodeWriterEncoding.QRCode 以生成 QR 碼。

  • SaveAsPng("MyBarCode.png") 將條碼圖像保存為 PNG 文件。

  • BarcodeImage 是表單上的 PictureBox 控件,用於向使用者顯示條碼圖像。

運行 .NET 條碼生成器

Ctrl + F5 運行應用程式。

條碼生成器 .NET 教學,圖 5:運行條碼生成器應用程式 運行條碼生成器應用程式

在文字框中輸入您想要編碼到條碼中的值,如下所示。

條碼生成器 .NET 教學,圖 6:粘貼 URL 以生成條碼 粘貼 URL 以生成條碼

現在,點擊 "生成" 按鈕。 條碼將如下面所示生成。

條碼生成器 .NET 教學,圖 7:在 Windows Forms 應用程式中生成的條碼 在 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();
' Add the encoded barcode value as text below the barcode image
MyBarCode.AddBarcodeValueTextBelowBarcode()
$vbLabelText   $csharpLabel

輸出

條碼生成器 .NET 教學,圖 8:從字串值生成條碼 從字串值生成條碼

總結

IronBarcode 為開發者提供了一個友好的 API,允許在 .NET 中讀取和寫入條碼,優化準確性並在現實軟體中確保低錯誤率。 造訪官方文件頁面以獲取有關 IronBarcode 的更多資訊。

目前,如果您購買完整的 Iron Suite,您可以以兩個程式庫的價格獲得五個程式庫。 如需更多資訊。

常見問題

如何在.NET中生成條碼?

您可以使用IronBarcode程式庫在.NET中生成條碼,首先需在Visual Studio中建立專案,安裝程式庫,設計UI,然後編寫程式碼來生成並顯示條碼。

條碼程式庫有哪些安裝方法?

您可以通過套件管理器控制台、NuGet套件管理器解決方案安裝IronBarcode程式庫,或者直接下載DLL並將其新增到您的專案中。

條碼生成器應用程式需要的UI元素有哪些?

條碼生成器應用程式的基本UI元素包括兩個標籤、用於輸入的豐富文字框及用於顯示生成的條碼影像的圖片框。

編寫條碼生成功能的步驟有哪些?

為實現條碼生成功能,需在button1_Click函式中編寫程式碼,使用IronBarcode生成條碼,將其保存為PNG文件,並顯示在圖片框中。

使用此程式庫可以生成哪種型別的條碼?

IronBarcode支持生成多種型別的條碼,包括Code128和QRCode等。

如何在生成的條碼下方新增文字?

您可以使用IronBarcode程式庫中的AddBarcodeValueTextBelowBarcode方法將條碼的編碼值作為文字新增到影像下方。

使用IronBarcode程式庫有哪些優勢?

使用IronBarcode提供了優化的條碼生成功能,具有高效能和準確度,使用者友好的API,以及在實際應用中的低錯誤率。

在哪裡可以查看條碼程式庫的詳細文件?

IronBarcode程式庫的詳細文件和範例可以在IronBarcode官方網站上找到。

條碼程式庫目前有促銷活動嗎?

是的,現在購買完整的Iron Suite可以以兩個程式庫的價格獲得五個程式庫。

如何排除.NET中條碼生成的常見問題?

常見問題往往可以通過確保正確安裝IronBarcode程式庫、檢查UI元件配置是否正確,以及驗證條碼生成程式碼無錯誤來解決。

Curtis Chau
技術作家

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

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

Iron 支援團隊

我們線上24小時,每週5天。
聊天
電子郵件
給我打電話