條碼產生器 .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
}在程式碼檔案頂部新增以下命名空間:
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 functionalityusing 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在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");讓我們逐行理解程式碼:
GeneratedBarcode是一種表示產生的條碼的資料類型。CreateBarcode是IronBarCode套件中的BarcodeWriter類別中的函數,用於根據使用者輸入產生條碼。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();輸出
條碼產生器 .NET 教程,圖 8:從字串值產生條碼 根據字串值產生條碼
摘要
IronBarcode 為開發人員提供了一個友善的 API,用於讀取和寫入 .NET 條碼,從而優化準確性並確保實際軟體中的低錯誤率。 請造訪官方文件頁面,以了解有關 IronBarcode 的更多資訊。
目前,如果您購買完整的 Iron Suite ,您可以以兩個庫的價格獲得五個庫。 了解更多。
常見問題解答
如何在.NET中產生條碼?
您可以使用 IronBarcode 庫在 .NET 中產生條碼,方法是在 Visual Studio 中建立一個項目,安裝該庫,設計 UI,然後編寫程式碼來產生和顯示條碼。
條碼庫有哪些安裝方法?
您可以使用套件管理器控制台、NuGet 套件管理器解決方案來安裝 IronBarcode 庫,或直接下載 DLL 並將其新增至您的專案。
條碼產生器應用程式必須具備哪些使用者介面元素?
條碼產生器應用程式的基本使用者介面元素包括兩個標籤、一個用於輸入的富文本框和一個用於顯示生成的條碼圖像的圖片框。
條碼產生功能的編碼涉及哪些步驟?
若要編寫條碼產生功能,請在button1_Click函數中編寫程式碼,使用 IronBarcode 產生條碼,將其儲存為 PNG 格式,並在 PictureBox 中顯示。
使用該庫可以產生哪些類型的條碼?
IronBarcode 支援產生各種條碼類型,包括 Code128 和 QRCode 等。
如何在產生的條碼下方新增文字?
您可以使用 IronBarcode 庫中的AddBarcodeValueTextBelowBarcode方法,將條碼的編碼值作為文字新增至影像下方。
使用 IronBarcode 函式庫有哪些優勢?
使用 IronBarcode 可實現高效能、高精度的最佳化條碼生成,提供用戶友好的 API,並且在實際應用中錯誤率低。
在哪裡可以找到條碼庫的詳細文件?
IronBarcode 庫的詳細文件和範例可在 IronBarcode 官方網站上找到。
條碼庫目前有任何促銷活動嗎?
是的,目前有促銷活動,購買完整的 Iron Suite 套裝即可享受五個音色庫的價格,相當於兩個音色庫的價格。
如何排查.NET中條碼產生的常見問題?
常見問題通常可以透過確保正確安裝 IronBarcode 庫、檢查 UI 元件是否正確配置以及驗證條碼產生程式碼是否沒有錯誤來解決。






