跳過到頁腳內容
使用IRONBARCODE

條碼生成器.NET教程

鑑於條碼使用的迅速攀升,開發人員必須能夠在其喜愛的編程語言中生成條碼。 因此,本教程將演示如何在.NET中生成條碼。

Barcode Generator .NET Tutorial

  1. 在Visual Studio中創建一個項目
  2. 安裝C#條碼生成器庫
  3. 為Windows窗體應用設計UI
  4. 為核心功能編寫代碼
  5. 運行.NET條碼生成器

讓我們開始教程。

創建項目

本教程使用最新版本的Visual Studio和Windows窗體應用模板。 您可以使用您選擇的應用並使用您的現有項目和版本。

打開Visual Studio > 點擊創建新項目 > 選擇Windows窗體應用模板 > 按下一步 > 命名項目 > 按下一步 => 選擇您的目標.NET框架 => 點擊創建按鈕。

條碼生成器.NET教程,圖1:創建一個新的Windows窗體應用 創建一個新的Windows窗體應用

安裝條碼庫

安裝條碼生成器庫有很多好處。 IronBarcode是用C#編寫的,提供用單行代碼創建條碼和QR碼的功能。 它還支持將QR碼或條碼保存為所需的文件格式。 此外,它還提供免費服務和運行時支持,以便在.NET中生成條碼。

讓我們從安裝IronBarcode NuGet包開始。 您可以通過以下三種方法之一進行安裝:

包管理器控制台

在包管理器控制台中寫入以下命令。 它將為您下載並安裝該包。

Install-Package BarCode

條碼生成器.NET教程,圖2:包管理器控制台安裝步驟 包管理器控制台安裝步驟

NuGet包管理方案

您還可以通過NuGet包方案安裝條碼包。 只需按照以下步驟進行:

點擊工具 > NuGet包管理器 > 管理解決方案的NuGet包

這將為您打開NuGet包管理器。 點擊瀏覽並搜索"IronBarCode",然後安裝該庫。

條碼生成器.NET教程,圖3:NuGet包管理器UI NuGet包管理器UI

從鏈接下載

作為替代方案,可以從IronBarCode.Dll下載並作為引用從.NET條碼DLL添加到您的項目中。

設計Windows窗體

.NET條碼生成器的UI應有2個標籤,1個豐富文本框和1個圖片框,以顯示生成的條碼圖像。 下圖展示了一個簡單的設計供演示之用。

條碼生成器.NET教程,圖4:設計Windows窗體應用程序 設計Windows窗體應用程序

編寫生成條碼的代碼

雙擊"生成"按鈕。 以下代碼將顯示:

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 是表示生成的條碼的數據類型。

  • CreateBarcodeIronBarCode包中BarcodeWriter類的函數,用於根據用戶輸入生成條碼。

  • 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窗體應用中生成的條碼 在Windows窗體應用中生成的條碼

顯示條碼的值

接下來,您可以通過單行代碼顯示條碼的值:

// 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 features a friendly API for developers to read and write barcodes for .NET, optimizing accuracy and ensuring a low error rate in real-world software. 訪問官方文檔頁面以獲取有關IronBarcode的更多信息。

目前,如果您購買完整的Iron Suite,您將以兩個的價格獲得五個庫。 更多信息。

常見問題解答

如何在 .NET 中生成條碼?

您可以使用 IronBarcode 庫在 .NET 中生成條碼,方法是創建 Visual Studio 項目,安裝庫,設計用戶界面,並編寫代碼來生成和顯示條碼。

條碼庫的安裝方法是什麼?

您可以通過程序包管理器控制台、NuGet 套件管理解決方案或直接下載 DLL 並將其添加到您的項目中來安裝 IronBarcode 庫。

條碼生成器應用程序的必要用戶界面元素是什麼?

條碼生成器應用程序的必要用戶界面元素包括兩個標籤、一個用於輸入的多行文本框和一個顯示生成的條碼圖像的圖片框。

編寫條碼生成函數需要哪些步驟?

要編寫條碼生成函數,請在 button1_Click 函數中編寫代碼,使用 IronBarcode 生成條碼,保存為 PNG 並顯示在 PictureBox 中。

使用該庫可以生成哪些類型的條碼?

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

我如何將文本添加到生成的條碼下面?

您可以使用 IronBarcode 庫中的 AddBarcodeValueTextBelowBarcode 方法將條碼的編碼值作為文本添加到圖像下方。

使用 IronBarcode 庫有什麼優勢?

使用 IronBarcode 提供優化的條碼生成,具有高性能和準確性,便於使用的 API,且實際應用中錯誤率低。

我在哪裡可以獲取條碼庫的詳細文檔?

條碼庫的詳細文檔和示例可以在 IronBarcode 官方網站上找到。

目前是否有任何條碼庫的促銷活動?

是的,有一個促銷活動,即購買完整的 Iron Suite,您可以以兩個價格獲得五個庫。

我如何解決 .NET 中條碼生成的常見問題?

常見問題通常可以通過確保正確安裝 IronBarcode 庫,檢查用戶界面元件是否正確配置,以及驗證條碼生成代碼是否無誤來解決。

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