如何使用 Iron BarCode 在 C# for .NET 5 中生成 QR 碼和條碼

C# 和 VB.NET 應用程式中的條碼和二維碼

This article was translated from English: Does it need improvement?
Translated
View the article in English

使用我們的 IronBarcode 軟體庫,在 C# 和所有其他 .NET 語言中讀取和寫入條碼非常簡單。

安裝 IronBarcode

旅程的第一步是安裝 IronBarcode,可以透過從 NuGet 下載或下載 DLL來完成。

若要安裝 IronBarcode NuGet 套件,您可以使用 Visual Studio 的 NuGet 套件管理器:

Install-Package BarCode

或者,您也可以使用 dotnet CLI 進行安裝:

dotnet add package IronBarCode

讀取條碼或二維碼

使用 IronBarcode 讀取條碼只需要一行程式碼。

:path=/static-assets/barcode/content-code-examples/get-started/get-started-1.cs
using IronBarCode;

BarcodeResults results = BarcodeReader.Read("QuickStart.jpg");
if (results != null)
{
    foreach (BarcodeResult result in results)
    {
        Console.WriteLine(result.Text);
    }
}
Imports IronBarCode

Private results As BarcodeResults = BarcodeReader.Read("QuickStart.jpg")
If results IsNot Nothing Then
	For Each result As BarcodeResult In results
		Console.WriteLine(result.Text)
	Next result
End If
$vbLabelText   $csharpLabel

只需一行程式碼,即可有效檢測並掃描輸入文件中的所有類型條碼——一步到位,滿足您的所有需求!此方法支援多種影像格式,例如 JPEG、PNG 和 BMP,以及 PDF 和多幀格式,例如 GIF 和 TIFF。 為了提高效能,提供了可自訂的配置選項。

為了提高讀取速度,您可以建立一個BarcodeReaderOptions對象,並將Speed設定配置為更好的效能。 預設為Balanced ,但可以選擇Faster選項來跳過某些檢查。

:path=/static-assets/barcode/content-code-examples/get-started/get-started-2.cs
using IronBarCode;

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    ExpectMultipleBarcodes = false,
    ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128,
    CropArea = new System.Drawing.Rectangle(100, 200, 300, 400),
};

BarcodeResults result = BarcodeReader.Read("QuickStart.jpg", myOptionsExample);
if (result != null)
{
    Console.WriteLine(result.First().Text);
}
Imports IronBarCode

Private myOptionsExample As New BarcodeReaderOptions() With {
	.ExpectMultipleBarcodes = False,
	.ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128,
	.CropArea = New System.Drawing.Rectangle(100, 200, 300, 400)
}

Private result As BarcodeResults = BarcodeReader.Read("QuickStart.jpg", myOptionsExample)
If result IsNot Nothing Then
	Console.WriteLine(result.First().Text)
End If
$vbLabelText   $csharpLabel

您也可以將ScanMode設定為OnlyBasicScan ,以最佳化讀取過程。

:path=/static-assets/barcode/content-code-examples/get-started/get-started-3.cs
using IronBarCode;

BarcodeResults results = BarcodeReader.Read("MultipleBarcodes.png");

// Loop through the results
foreach (BarcodeResult result in results)
{
    string value = result.Value;
    Bitmap img = result.BarcodeImage;
    BarcodeEncoding barcodeType = result.BarcodeType;
    byte[] binary = result.BinaryValue;
    Console.WriteLine(result.Value);
}
Imports IronBarCode

Private results As BarcodeResults = BarcodeReader.Read("MultipleBarcodes.png")

' Loop through the results
For Each result As BarcodeResult In results
	Dim value As String = result.Value
	Dim img As Bitmap = result.BarcodeImage
	Dim barcodeType As BarcodeEncoding = result.BarcodeType
	Dim binary() As Byte = result.BinaryValue
	Console.WriteLine(result.Value)
Next result
$vbLabelText   $csharpLabel

其他配置包括指定要掃描的條碼格式,這可以透過減少不必要的掃描來加快處理速度。

:path=/static-assets/barcode/content-code-examples/get-started/get-started-4.cs
using IronBarCode;

BarcodeResults pagedResults = BarcodeReader.Read("MultipleBarcodes.pdf");

// Loop through the results
foreach (BarcodeResult result in pagedResults)
{
    int pageNumber = result.PageNumber;
    string value = result.Value;
    Bitmap img = result.BarcodeImage;
    BarcodeEncoding barcodeType = result.BarcodeType;
    byte[] binary = result.BinaryValue;
    Console.WriteLine(result.Value);
}

// or from a multi-page  TIFF scan with image correction:
BarcodeResults multiFrameResults = BarcodeReader.Read(inputImage: "Multiframe.tiff", new BarcodeReaderOptions
{
    Speed = ReadingSpeed.Detailed,
    ExpectMultipleBarcodes = true,
    ExpectBarcodeTypes = BarcodeEncoding.Code128,
    Multithreaded = false,
    RemoveFalsePositive = false,
    ImageFilters = null
});
Imports IronBarCode

Private pagedResults As BarcodeResults = BarcodeReader.Read("MultipleBarcodes.pdf")

' Loop through the results
For Each result As BarcodeResult In pagedResults
	Dim pageNumber As Integer = result.PageNumber
	Dim value As String = result.Value
	Dim img As Bitmap = result.BarcodeImage
	Dim barcodeType As BarcodeEncoding = result.BarcodeType
	Dim binary() As Byte = result.BinaryValue
	Console.WriteLine(result.Value)
Next result

' or from a multi-page  TIFF scan with image correction:
Dim multiFrameResults As BarcodeResults = BarcodeReader.Read(inputImage:= "Multiframe.tiff", New BarcodeReaderOptions With {
	.Speed = ReadingSpeed.Detailed,
	.ExpectMultipleBarcodes = True,
	.ExpectBarcodeTypes = BarcodeEncoding.Code128,
	.Multithreaded = False,
	.RemoveFalsePositive = False,
	.ImageFilters = Nothing
})
$vbLabelText   $csharpLabel

條碼編寫

要使用 IronBarcode 來寫條碼,我們使用BarcodeWriter類別。

:path=/static-assets/barcode/content-code-examples/get-started/get-started-5.cs
using IronBarCode;

GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128);
myBarcode.SaveAsImage("myBarcode.png");
Imports IronBarCode

Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128)
myBarcode.SaveAsImage("myBarcode.png")
$vbLabelText   $csharpLabel

條碼樣式

IronBarcode 提供了多種選項來操控條碼的視覺呈現。

:path=/static-assets/barcode/content-code-examples/get-started/get-started-7.cs
using IronBarCode;

GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128);
myBarcode.AddAnnotationTextAboveBarcode("Product URL:");
myBarcode.AddBarcodeValueTextBelowBarcode();
myBarcode.SetMargins(100);
myBarcode.ChangeBarCodeColor(IronSoftware.Drawing.Color.Purple);

// All major image formats supported as well as PDF and HTML
myBarcode.SaveAsPng("myBarcode.png");
Imports IronBarCode

Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128)
myBarcode.AddAnnotationTextAboveBarcode("Product URL:")
myBarcode.AddBarcodeValueTextBelowBarcode()
myBarcode.SetMargins(100)
myBarcode.ChangeBarCodeColor(IronSoftware.Drawing.Color.Purple)

' All major image formats supported as well as PDF and HTML
myBarcode.SaveAsPng("myBarcode.png")
$vbLabelText   $csharpLabel

將條碼匯出為 HTML

IronBarcode 可以將條碼匯出為 HTML 文件或 HTML 內容的一部分。

:path=/static-assets/barcode/content-code-examples/get-started/get-started-8.cs
using IronBarCode;

QRCodeWriter.CreateQrCode("https://ironsoftware.com", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPdf("MyQR.pdf");
Imports IronBarCode

QRCodeWriter.CreateQrCode("https://ironsoftware.com", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPdf("MyQR.pdf")
$vbLabelText   $csharpLabel

產生二維碼

對於二維碼,請使用QRCodeWriter類,該類別提供了針對二維碼特定功能(例如糾錯)的額外配置。

:path=/static-assets/barcode/content-code-examples/get-started/get-started-9.cs
using IronBarCode;
using IronSoftware.Drawing;

QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png");
GeneratedBarcode myQRCodeWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", qrCodeLogo);
myQRCodeWithLogo.ChangeBarCodeColor(Color.DarkGreen).SaveAsPdf("MyQRWithLogo.pdf");
Imports IronBarCode
Imports IronSoftware.Drawing

Private qrCodeLogo As New QRCodeLogo("visual-studio-logo.png")
Private myQRCodeWithLogo As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", qrCodeLogo)
myQRCodeWithLogo.ChangeBarCodeColor(Color.DarkGreen).SaveAsPdf("MyQRWithLogo.pdf")
$vbLabelText   $csharpLabel

支援的條碼格式

IronBarcode支援多種常用條碼格式的讀取和寫入:

  • QR碼微型QR碼矩形微型QR碼(rMQR碼)
  • 其他二維條碼,例如AztecData MatrixMaxiCodePDF417
  • 堆疊式線性條碼,例如Databar
  • 傳統的一維條碼格式,例如UPC-AUPC-EEAN-8EAN-13CodabarITFMSIPlessey

為什麼選擇 IronBarcode?

IronBarcode 為開發人員提供了一個友好、易於使用的 API,用於讀取和寫入 .NET 條碼,該 API 在實際使用場景中針對準確性、精確性和速度進行了最佳化。

例如, BarcodeWriter類別會自動驗證和修正 UPCA 和 UPCE 條碼的"校驗和",並處理數位格式約束。 IronBarcode 可協助開發人員選擇最適合其資料的條碼格式。

該庫功能強大,具有自動旋轉和影像去噪等影像預處理技術,可最大限度地提高條碼檢測成功率。

展望未來

為了充分利用 IronBarcode,我們鼓勵您閱讀本文檔部分的教學課程,並造訪我們的GitHub 頁面

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 1,979,979 | Version: 2025.11 剛發表