Barcodes & QRs in C# & VB.NET Applications
透過我們的 IronBarcode 程式庫,在 C# 及所有其他 .NET 語言中讀取與寫入 BarCode 變得輕而易舉。
安裝 IronBarcode
這段旅程的第一步是安裝 IronBarcode,您可以透過 NuGet 下載,或直接下載 DLL 檔案。
若要安裝 IronBarcode NuGet 套件,您可以使用 Visual Studio 的 NuGet 套件管理員:
Install-Package BarCode
此外,您也可以透過 .NET CLI 進行安裝:
dotnet add package IronBarCode
讀取 BarCode 或 QR 碼
using IronBarcode 讀取 BARCODE 只需一行程式碼。
: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
只需這一行程式碼,您便能以卓越的效能偵測並掃描輸入文件中的所有類型 BARCODE——一步到位,滿足您的一切需求!此方法支援多種影像格式,例如 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
您亦可將 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
其他設定包含指定要掃描的BarCode格式,這有助於減少不必要的掃描,從而加快處理速度。
: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
})
BarCode編寫
若要使用 IronBarcode 繪製 BARCODE,我們會使用 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")
BarCode樣式設定
IronBarcode 提供多種選項,可對 BARCODE 的視覺呈現進行調整。
: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")
將 BarCode 匯出為 HTML
IronBarcode 可將 BarCode 匯出為 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")
生成 QR 碼
針對 QR 碼,請使用 QRCodeWriter 類別,該類別提供針對 QR 碼特定功能(如錯誤校正)的額外設定選項。
: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")
支援的BarCode格式
IronBarcode 支援多種常見 BarCode 格式的讀取與寫入:
- QR 碼、Micro QR 碼及矩形 Micro QR 碼 (rMQR)。
- 其他二維 BARCODE,例如 Aztec、Data Matrix、MaxiCode 及 PDF417。
- 堆疊式線性BarCode,例如 Databar。
- 傳統的一維 BarCode 格式,例如 UPC-A、UPC-E、EAN-8、EAN-13、Codabar、ITF、MSI 及 Plessey。
為何選擇 IronBarcode?
IronBarcode 提供一個友善且易於使用的 API,讓開發人員能夠在 .NET 環境中讀取和寫入 BARCODE,並針對實際應用情境優化了準確性、精確度與速度。
例如,BarcodeWriter 類別會自動驗證並修正 UPCA 和 UPCE BARCODE 上的"校驗和",並處理數值格式限制。 IronBarcode 協助開發人員為其資料選擇最適合的 BarCode 格式。
此函式庫功能強大,具備自動旋轉與影像去噪等預處理技術,以最大化BARCODE偵測的成功率。
後續進行
為充分發揮 IronBarcode 的功能,我們建議您閱讀本文件區段中的教學指南,並造訪我們的 GitHub 頁面。

