跳過到頁腳內容
使用IRONBARCODE

條碼生成器.NET教程

由於條碼使用的快速上升,開發人員必須能夠在他們首選的程式語言中生成條碼。 因此,本教程將演示如何在.NET中生成條碼。

條碼生成器.NET教程

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

讓我們開始教程吧。

創建專案

本教程使用最新版本的Visual Studio和Windows Forms應用程式範本。 您可以使用您選擇的應用程式,也可以使用您現有的專案和版本。

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

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

安裝條碼程式庫

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

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

Package Manager Console

在Package Manager Console中寫以下命令。 它會為您下載並安裝這個套件。

Install-Package BarCode

條碼生成器.NET教程,圖2:Package Manager Console安裝步驟 Package Manager Console安裝步驟

NuGet Package Manager解決方案

您還可以使用NuGet Package Solution安裝條碼套件。 只需遵循這些步驟:

點擊工具 > NuGet Package Manager > 管理Solution的NuGet套件

這將為您打開NuGet Package Manager。 點擊Browse,然後搜尋"IronBarCode",然後安裝程式庫。

條碼生成器.NET教程,圖3:NuGet Package Manager UI NuGet Package Manager UI

從鏈接下載

作為替代方案,可以下載IronBarCode.Dll並從.NET Barcode DLL中將其作為引用添加到您的專案中。

設計Windows Forms

.NET條碼生成器的UI應該有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
}
$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
$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");
$vbLabelText   $csharpLabel

讓我們逐行理解程式碼:

  • GeneratedBarcode是一個表示生成條碼的數據類型。

  • 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();
$vbLabelText   $csharpLabel

輸出

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

總結

IronBarcode具有友好的API,開發人員可以讀取和編寫.NET條碼,優化準確性並確保實際軟體中的低錯誤率。 訪問官方文檔頁面以獲取更多有關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 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。

Iron Support Team

We're online 24 hours, 5 days a week.
Chat
Email
Call Me