條碼生成器.NET教程
鑑於條碼使用量的迅速增長,開發人員必須能夠使用他們喜歡的程式語言來產生條碼。 因此,本教學將示範如何在 .NET 中產生條碼。
條碼產生器 .NET 教學課程
- 在 Visual Studio 中建立項目
- 安裝 C# 條碼產生器庫
- 設計 Windows 窗體應用程式的使用者介面
- 編寫核心功能的程式碼
- 執行 .NET 條碼產生器
讓我們開始教學。
創建專案
本教學課程使用最新版本的 Visual Studio 和 Windows 窗體應用程式範本。 您可以選擇使用您喜歡的應用程式,並使用您現有的項目和版本。
開啟 Visual Studio > 按一下"建立新專案" > 選擇"Windows 表單應用程式範本" > 按下"下一步" > 為專案命名 > 按"下一步" => 選擇目標 .NET Framework => 按一下"建立"按鈕。
條碼產生器 .NET 教程,圖 1:建立一個新的 Windows 窗體應用程式 建立一個新的 Windows 窗體應用程式
安裝條碼庫
安裝條碼產生器庫有很多好處。 IronBarcode 是用 C# 編寫的,它提供了只需一行程式碼即可建立條碼和二維碼的功能。 它還支援以所需的檔案格式儲存二維碼或條碼。 此外,它還為在 .NET 中產生條碼提供免費服務和運行時支援。
我們先來安裝 IronBarcode NuGet 套件。 您可以使用以下三種方法之一進行安裝:
套件管理員控制台
在軟體包管理器控制台中輸入以下命令。 它將為您下載並安裝該軟體包。
Install-Package BarCode
條碼產生器 .NET 教程,圖 2:套件管理器控制台安裝步驟 軟體套件管理器控制台安裝步驟
NuGet 套件管理器解決方案
您也可以使用 NuGet 套件解決方案安裝條碼套件。 只需按照以下步驟操作:
按一下"工具" > "NuGet 套件管理員" > "管理解決方案的 NuGet 套件" 。
這將開啟 NuGet 套件管理器。 點擊"瀏覽"並蒐索"IronBarCode",然後安裝該程式庫。
條碼產生器 .NET 教程,圖 3:NuGet 套件管理器使用者介面 NuGet 套件管理器使用者介面
從連結下載
作為替代方案,您可以從 .NET 條碼 DLL 下載IronBarCode.Dll並將其作為參考新增至您的專案。
設計 Windows 窗體
.NET 條碼產生器的使用者介面應包含 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
在程式碼檔案頂部新增以下命名空間:
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
在 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")
讓我們逐行理解程式碼:
-
GeneratedBarcode是一種表示產生的條碼的資料型別。 -
CreateBarcode是BarcodeWriter類別中的函數,位於IronBarCode套件中,用於根據使用者輸入產生條碼。 -
BarcodeValue.Text取得使用者輸入的文本,該文本將被編碼到條碼中。 -
BarcodeWriterEncoding.Code128指定產生條碼的編碼方案。 您可以將其變更為其他編碼類型,例如BarcodeWriterEncoding.QRCode,以產生二維碼。 -
SaveAsPng("MyBarCode.png")將條碼圖片儲存為 PNG 檔案。 BarcodeImage是窗體上的 PictureBox 控件,用於向使用者顯示條碼圖像。
執行 .NET 條碼產生器
按 Ctrl + F5 運行應用程式。
條碼產生器 .NET 教程,圖 5:運行條碼生成器應用程式 運行條碼產生器應用程式
請在文字方塊中輸入您想要編碼到條碼中的值,如下所示。
條碼產生器 .NET 教程,圖 6:貼上 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()
輸出
條碼產生器 .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 庫,檢查用戶界面元件是否正確配置,以及驗證條碼生成代碼是否無誤來解決。

