
如何在ASP.NET中使用C#生成條碼
本教程將演示如何在C# ASP.NET中使用IronBarcode程式庫生成條碼。 使用此.NET程式庫,您可以輕鬆生成條碼、設計樣式,並將其匯出為圖像、PDF或HTML。
如何在C# .NET應用程式中生成條碼
- 在Microsoft Visual Studio中建立一個Console .NET專案
- 安裝條碼程式庫
- 從條碼套件匯入命名空間
- 生成條碼圖像
- 條碼圖像樣式化
- 流暢的條碼生成
1. 在Microsoft Visual Studio中建立一個Console .NET應用程式
本教程使用Visual Studio的最新版本和Console Application (.NET Core)模板。 它也與Windows Forms和ASP.NET Web應用程式相容。
打開Visual Studio > 點擊建立新專案 > 選擇Console App (.NET) > 按下一步 > 輸入專案名稱 > 按下一步 > 選擇您的目標.NET Framework > 點擊建立按鈕。
建立專案後,從Visual Studio工具箱設計表單:標籤、文字框和按鈕控制項。
建立Console App
2. 在C#中安裝條碼生成程式庫
IronBarcode程式庫可以使用以下三種方法之一進行安裝:
1. 包管理器控制台
在包管理器控制台中輸入以下命令。 它會為您下載並安裝該套件。
套件管理器主控台安裝步驟
2. NuGet包管理解決方案
您也可以使用NuGet包管理解決方案來安裝條碼程式庫。 只需遵循以下步驟:
點擊 工具 > NuGet 套件管理器 > 為解決方案管理 NuGet 套件。
這將為您打開NuGet包管理。 點擊瀏覽並搜尋條碼,然後安裝程式庫。 或者,您可以在解決方案資源管理器中點擊新增 > 專案參考,以將類程式庫新增到條碼生成中。
條碼搜尋
3. 從連結下載
作為替代方案,可以下載IronBarCode.Dll,並作為參考新增到您的專案中。
3. 匯入命名空間
為了確保類文件引用IronBarcode程式庫和某些標準系統組件,請使用這些命名空間。
using IronBarCode;
using System;
using System.Drawing;
using System.Linq;Imports IronBarCode
Imports System
Imports System.Drawing
Imports System.Linq4. 生成條碼圖像
在以下範例程式碼中,您可以使用單行程式碼生成包含數字或文字內容的條碼圖像。 您也可以將它們儲存為PNG圖像檔,並在您的應用程式中查看它們。
// Generate a Simple Barcode image and save as PNG
GeneratedBarcode barCode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.Code128);
barCode.SaveAsPng("BarCode.png");
// This line opens the image in your default image viewer
System.Diagnostics.Process.Start("BarCode.png");' Generate a Simple Barcode image and save as PNG
Dim barCode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.Code128)
barCode.SaveAsPng("BarCode.png")
' This line opens the image in your default image viewer
System.Diagnostics.Process.Start("BarCode.png")上述程式碼生成條碼,輸出如下:
在C#範例中建立條碼圖像
最後一行程式碼只是打開預設圖像查看器中的條碼PNG,使您可以在條碼生成器輸出中看到它。
5. 條碼圖像樣式化
在以下範例程式碼中,您將看到如何向條碼新增註釋。 您可以設置字體,顯示其下方的值,新增邊距,更改條碼顏色,然後儲存,一切都可以在C#中簡單完成。 最後,您可以輕鬆地將其儲存為各種圖像檔。
如果這對您的應用程式更合適,您也可以選擇匯出為HTML或PDF而不是圖像。
// Styling a QR code and adding annotation text
var barcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.QRCode);
barcode.AddAnnotationTextAboveBarcode("Product URL:");
barcode.AddBarcodeValueTextBelowBarcode();
barcode.SetMargins(100);
barcode.ChangeBarCodeColor(Color.Green);
// Save as HTML
barcode.SaveAsHtmlFile("MyBarCode.html"); ' Styling a QR code and adding annotation text
Dim barcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.QRCode)
barcode.AddAnnotationTextAboveBarcode("Product URL:")
barcode.AddBarcodeValueTextBelowBarcode()
barcode.SetMargins(100)
barcode.ChangeBarCodeColor(Color.Green)
' Save as HTML
barcode.SaveAsHtmlFile("MyBarCode.html")
使用C#建立帶有註釋和樣式化的條碼圖像
程式碼應該是自我解釋的; 然而,GeneratedBarcode類文件中的說明可以在API Reference中提供其他技術資訊。
此外,IronBarcode還支持從圖像中讀取條碼,以及提供額外選項來更精確地讀取條碼或對圖像應用濾鏡。
6. 條碼生成的流暢性
IronBarcode實現了一種類似於System.Linq的可選Fluent API,用於以下順序的連結方法調用:建立條碼、設置其邊距,然後在單行中將其匯出到Bitmap。
這非常方便,使程式碼更易於閱讀。
// Fluent API for Barcode Image generation.
string myValue = "https://ironsoftware.com/csharp/barcode";
Bitmap barcodeBmp = BarcodeWriter.CreateBarcode(myValue, BarcodeEncoding.PDF417)
.ResizeTo(300, 200)
.SetMargins(100)
.ToBitmap();' Fluent API for Barcode Image generation.
Dim myValue As String = "https://ironsoftware.com/csharp/barcode"
Dim barcodeBmp As Bitmap = BarcodeWriter.CreateBarcode(myValue, BarcodeEncoding.PDF417).ResizeTo(300, 200).SetMargins(100).ToBitmap()結果是System.Drawing.Image一個PDF417條碼,樣子如下:
使用IronBarcode在C#中簡單而流暢的條碼生成
7. 總結
IronBarcode為開發人員提供了友好的API,來讀取和生成C# .NET的條碼圖像和QR碼,優化準確性,並確保在現實世界中使用時的低錯誤率。 您還可以列印條碼圖像。 存取官方文件頁面以獲取有關IronBarcode的更多資訊。
目前,如果您購買完整的Iron Suite,您可以以兩個價格獲得五個程式庫。

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


