Generate QR Codes in C# - Complete Tutorial for .NET Developers
需要在您的 C# 應用程式中產生 QR 碼嗎? 本教學將詳細示範如何使用 IronBarcode 來建立、自訂及驗證 QR 碼——從簡單的一行程式碼實作,到嵌入商標與二進位資料編碼等進階功能。
無論您正在建置庫存系統、活動票務平台,還是非接觸式支付解決方案,您都將學會如何在 .NET 應用程式中實作 Professional 級的 QR 碼功能。
快速入門:使用 IronBarcode 建立單行 QR 碼
準備好快速生成 QR 碼了嗎?以下說明如何使用 IronBarcode 的 QRCodeWriter API,僅需一行程式碼即可生成 QR 碼——自訂功能雖為選用,但功能強大。
簡化工作流程(5 個步驟)
- 透過 NuGet 安裝 IronBarcode
- 建立一行文字的 QR 碼:
QRCodeWriter.CreateQrCode() - 使用
CreateQrCodeWithLogo() - 請使用
GeneratedBarcode.Verify() - 為進階應用程式編碼二進位資料
How Do I Install a QR Code Library in C#?
請透過 NuGet 套件管理員,使用以下簡單指令安裝 IronBarcode:
Install-Package BarCode
或者,您可以直接下載 IronBarcode DLL 並將其新增為專案的參考。
導入所需命名空間
請新增以下命名空間以存取 IronBarcode 的 QR 碼產生功能:
using IronBarCode;
using System;
using System.Drawing;
using System.Linq;
using IronBarCode;
using System;
using System.Drawing;
using System.Linq;
Imports IronBarCode
Imports System
Imports System.Drawing
Imports System.Linq
How Can I Create a Simple QR Code in C#?
using IronBarcode 的 CreateQrCode 方法,僅需一行程式碼即可產生 QR 碼:
using IronBarCode;
// Generate a QR code with text content
var qrCode = QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium);
qrCode.SaveAsPng("MyQR.png");
using IronBarCode;
// Generate a QR code with text content
var qrCode = QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium);
qrCode.SaveAsPng("MyQR.png");
Imports IronBarCode
' Generate a QR code with text content
Private qrCode = QRCodeWriter.CreateQrCode("hello world", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium)
qrCode.SaveAsPng("MyQR.png")
`` 方法接受三個參數:
- 文字內容:待編碼的資料(支援 URL、文字或任何字串資料)
- 尺寸:方形 QR 碼的像素尺寸(本例為 500x500)
- 錯誤修正:評估在非理想條件下的可讀性(低、中、四分位數或高)
較高的錯誤修正等級可讓 QR 碼即使部分受損或被遮蔽仍能保持可讀性,但這會導致圖案更密集且包含更多資料模組。
一個包含"hello world"文字的基本 QR 碼,以 500x500 像素生成,並採用中等錯誤修正等級
如何將標誌加入我的 QR 碼?
將標誌嵌入 QR 碼中,既能提升品牌辨識度,又能維持掃描的便利性。 IronBarcode 會自動調整標誌的位置與大小,以確保 QR 碼的完整性:
using IronBarCode;
using IronSoftware.Drawing;
// Load logo image
QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png");
// Create QR code with embedded logo
GeneratedBarcode myQRCodeWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo);
// Customize appearance
myQRCodeWithLogo.ResizeTo(500, 500).SetMargins(10).ChangeBarCodeColor(Color.DarkGreen);
// Save the branded QR code
myQRCodeWithLogo.SaveAsPng("myQRWithLogo.png");
using IronBarCode;
using IronSoftware.Drawing;
// Load logo image
QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png");
// Create QR code with embedded logo
GeneratedBarcode myQRCodeWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo);
// Customize appearance
myQRCodeWithLogo.ResizeTo(500, 500).SetMargins(10).ChangeBarCodeColor(Color.DarkGreen);
// Save the branded QR code
myQRCodeWithLogo.SaveAsPng("myQRWithLogo.png");
Imports IronBarCode
Imports IronSoftware.Drawing
' Load logo image
Private qrCodeLogo As New QRCodeLogo("visual-studio-logo.png")
' Create QR code with embedded logo
Private myQRCodeWithLogo As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo)
' Customize appearance
myQRCodeWithLogo.ResizeTo(500, 500).SetMargins(10).ChangeBarCodeColor(Color.DarkGreen)
' Save the branded QR code
myQRCodeWithLogo.SaveAsPng("myQRWithLogo.png")
`` 方法透過以下方式智慧地處理標誌定位:
- 自動調整標誌大小以確保 QR 碼的可讀性
- 將其定位於靜區以避免資料損毀
- 變更 QR 碼顏色時,須保留標誌的原始色彩
此方法可確保您的品牌 QR 碼在所有掃描裝置和應用程式中均能正常運作。
展示 Visual Studio 標誌的 QR 碼,展示 IronBarcode 的自動標誌尺寸調整與定位功能
如何將 QR 碼匯出為不同格式?
IronBarcode 支援多種匯出格式,以滿足不同使用情境的需求。 將您的 QR 碼匯出為圖片、PDF 或 HTML 檔案:
using IronBarCode;
using System.Drawing;
// Create QR code with logo
QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png");
GeneratedBarcode myQRCodeWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo);
// Apply custom styling
myQRCodeWithLogo.ChangeBarCodeColor(Color.DarkGreen);
// Export to multiple formats
myQRCodeWithLogo.SaveAsPdf("MyQRWithLogo.pdf"); // PDF document
myQRCodeWithLogo.SaveAsHtmlFile("MyQRWithLogo.html"); // Standalone HTML
myQRCodeWithLogo.SaveAsPng("MyQRWithLogo.png"); // PNG image
myQRCodeWithLogo.SaveAsJpeg("MyQRWithLogo.jpg"); // JPEG image
using IronBarCode;
using System.Drawing;
// Create QR code with logo
QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png");
GeneratedBarcode myQRCodeWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo);
// Apply custom styling
myQRCodeWithLogo.ChangeBarCodeColor(Color.DarkGreen);
// Export to multiple formats
myQRCodeWithLogo.SaveAsPdf("MyQRWithLogo.pdf"); // PDF document
myQRCodeWithLogo.SaveAsHtmlFile("MyQRWithLogo.html"); // Standalone HTML
myQRCodeWithLogo.SaveAsPng("MyQRWithLogo.png"); // PNG image
myQRCodeWithLogo.SaveAsJpeg("MyQRWithLogo.jpg"); // JPEG image
Imports IronBarCode
Imports System.Drawing
' Create QR code with logo
Private qrCodeLogo As New QRCodeLogo("visual-studio-logo.png")
Private myQRCodeWithLogo As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo)
' Apply custom styling
myQRCodeWithLogo.ChangeBarCodeColor(Color.DarkGreen)
' Export to multiple formats
myQRCodeWithLogo.SaveAsPdf("MyQRWithLogo.pdf") ' PDF document
myQRCodeWithLogo.SaveAsHtmlFile("MyQRWithLogo.html") ' Standalone HTML
myQRCodeWithLogo.SaveAsPng("MyQRWithLogo.png") ' PNG image
myQRCodeWithLogo.SaveAsJpeg("MyQRWithLogo.jpg") ' JPEG image
每種格式皆有其特定用途:
- PDF:適用於可列印的文件與報告
- HTML:無需外部依賴項,完美適用於網頁整合
- PNG/JPEG:用途廣泛的標準圖像格式
如何在自訂後驗證 QR 碼的可讀性?
顏色調整及標誌添加可能會影響 QR 碼的掃描效果。 請使用 `` 方法,以確保您自訂的 QR 碼仍可讀取:
using IronBarCode;
using IronSoftware.Drawing;
using System;
using System.Drawing;
// Generate QR code with logo
QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png");
GeneratedBarcode myVerifiedQR = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo);
// Apply light color (may affect readability)
myVerifiedQR.ChangeBarCodeColor(Color.LightBlue);
// Verify the QR code can still be scanned
if (!myVerifiedQR.Verify())
{
Console.WriteLine("LightBlue is not dark enough to be read accurately. Let's try DarkBlue");
myVerifiedQR.ChangeBarCodeColor(Color.DarkBlue);
}
// Save verified QR code
myVerifiedQR.SaveAsHtmlFile("MyVerifiedQR.html");
using IronBarCode;
using IronSoftware.Drawing;
using System;
using System.Drawing;
// Generate QR code with logo
QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png");
GeneratedBarcode myVerifiedQR = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo);
// Apply light color (may affect readability)
myVerifiedQR.ChangeBarCodeColor(Color.LightBlue);
// Verify the QR code can still be scanned
if (!myVerifiedQR.Verify())
{
Console.WriteLine("LightBlue is not dark enough to be read accurately. Let's try DarkBlue");
myVerifiedQR.ChangeBarCodeColor(Color.DarkBlue);
}
// Save verified QR code
myVerifiedQR.SaveAsHtmlFile("MyVerifiedQR.html");
Imports IronBarCode
Imports IronSoftware.Drawing
Imports System
Imports System.Drawing
' Generate QR code with logo
Dim qrCodeLogo As New QRCodeLogo("visual-studio-logo.png")
Dim myVerifiedQR As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo)
' Apply light color (may affect readability)
myVerifiedQR.ChangeBarCodeColor(Color.LightBlue)
' Verify the QR code can still be scanned
If Not myVerifiedQR.Verify() Then
Console.WriteLine("LightBlue is not dark enough to be read accurately. Let's try DarkBlue")
myVerifiedQR.ChangeBarCodeColor(Color.DarkBlue)
End If
' Save verified QR code
myVerifiedQR.SaveAsHtmlFile("MyVerifiedQR.html")
`` 方法會對您的 QR 碼執行全面的掃描測試。 這可確保在部署前,系統能與各種掃描裝置及光線條件相容。
一個已成功驗證的深藍色 QR 碼,展示出適當的對比度以確保可靠掃描
如何將二進位資料編碼至 QR 碼中?
QR 碼在高效儲存二進位資料方面表現出色。 此功能可實現加密資料傳輸、檔案共享及物聯網裝置設定等進階應用:
using IronBarCode;
using System;
using System.Linq;
// Convert string to binary data
byte[] binaryData = System.Text.Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/barcode/");
// Create QR code from binary content
QRCodeWriter.CreateQrCode(binaryData, 500).SaveAsPng("MyBinaryQR.png");
// Read and verify binary data integrity
var myReturnedData = BarcodeReader.Read("MyBinaryQR.png").First();
// Confirm data matches original
if (binaryData.SequenceEqual(myReturnedData.BinaryValue))
{
Console.WriteLine("Binary Data Read and Written Perfectly");
}
else
{
throw new Exception("Data integrity check failed");
}
using IronBarCode;
using System;
using System.Linq;
// Convert string to binary data
byte[] binaryData = System.Text.Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/barcode/");
// Create QR code from binary content
QRCodeWriter.CreateQrCode(binaryData, 500).SaveAsPng("MyBinaryQR.png");
// Read and verify binary data integrity
var myReturnedData = BarcodeReader.Read("MyBinaryQR.png").First();
// Confirm data matches original
if (binaryData.SequenceEqual(myReturnedData.BinaryValue))
{
Console.WriteLine("Binary Data Read and Written Perfectly");
}
else
{
throw new Exception("Data integrity check failed");
}
Imports IronBarCode
Imports System
Imports System.Linq
' Convert string to binary data
Private binaryData() As Byte = System.Text.Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/barcode/")
' Create QR code from binary content
QRCodeWriter.CreateQrCode(binaryData, 500).SaveAsPng("MyBinaryQR.png")
' Read and verify binary data integrity
Dim myReturnedData = BarcodeReader.Read("MyBinaryQR.png").First()
' Confirm data matches original
If binaryData.SequenceEqual(myReturnedData.BinaryValue) Then
Console.WriteLine("Binary Data Read and Written Perfectly")
Else
Throw New Exception("Data integrity check failed")
End If
QR 碼中的二進位編碼具備以下幾項優勢:
- 效率:以緊湊的二進位格式儲存資料
- 多功能性:可處理任何資料類型(檔案、加密內容、序列化物件)
- 完整性:保留精確的位元組序列,且無編碼問題
此功能使 IronBarcode 有別於基礎的 QR 碼函式庫,讓您的應用程式能夠實現複雜的資料交換情境。
儲存二進位資料的 QR 碼,展示 IronBarcode 的先進編碼能力
How Do I Read QR Codes in C#?
IronBarcode 提供靈活的 QR 碼讀取功能。 以下是最簡潔的翻譯方式:
using IronBarCode;
using System;
using System.Linq;
// Read QR code with optimized settings
BarcodeResults result = BarcodeReader.Read("QR.png", new BarcodeReaderOptions() {
ExpectBarcodeTypes = BarcodeEncoding.QRCode
});
// Extract and display the decoded value
if (result != null && result.Any())
{
Console.WriteLine(result.First().Value);
}
else
{
Console.WriteLine("No QR codes found in the image.");
}
using IronBarCode;
using System;
using System.Linq;
// Read QR code with optimized settings
BarcodeResults result = BarcodeReader.Read("QR.png", new BarcodeReaderOptions() {
ExpectBarcodeTypes = BarcodeEncoding.QRCode
});
// Extract and display the decoded value
if (result != null && result.Any())
{
Console.WriteLine(result.First().Value);
}
else
{
Console.WriteLine("No QR codes found in the image.");
}
Imports IronBarCode
Imports System
Imports System.Linq
' Read QR code with optimized settings
Private result As BarcodeResults = BarcodeReader.Read("QR.png", New BarcodeReaderOptions() With {.ExpectBarcodeTypes = BarcodeEncoding.QRCode})
' Extract and display the decoded value
If result IsNot Nothing AndAlso result.Any() Then
Console.WriteLine(result.First().Value)
Else
Console.WriteLine("No QR codes found in the image.")
End If
針對需要精細控制的更複雜情境:
using IronBarCode;
using System;
using System.Linq;
// Configure advanced reading options
BarcodeReaderOptions options = new BarcodeReaderOptions
{
Speed = ReadingSpeed.Faster, // Optimize for speed
ExpectMultipleBarcodes = false, // Single QR code expected
ExpectBarcodeTypes = BarcodeEncoding.QRCode, // QR codes only
Multithreaded = true, // Enable parallel processing
MaxParallelThreads = 4, // Utilize multiple CPU cores
RemoveFalsePositive = true, // Filter out false detections
ImageFilters = new ImageFilterCollection() // Apply preprocessing
{
new AdaptiveThresholdFilter(), // Handle varying lighting
new ContrastFilter(), // Enhance contrast
new SharpenFilter() // Improve edge definition
}
};
// Read with advanced configuration
BarcodeResults result = BarcodeReader.Read("QR.png", options);
using IronBarCode;
using System;
using System.Linq;
// Configure advanced reading options
BarcodeReaderOptions options = new BarcodeReaderOptions
{
Speed = ReadingSpeed.Faster, // Optimize for speed
ExpectMultipleBarcodes = false, // Single QR code expected
ExpectBarcodeTypes = BarcodeEncoding.QRCode, // QR codes only
Multithreaded = true, // Enable parallel processing
MaxParallelThreads = 4, // Utilize multiple CPU cores
RemoveFalsePositive = true, // Filter out false detections
ImageFilters = new ImageFilterCollection() // Apply preprocessing
{
new AdaptiveThresholdFilter(), // Handle varying lighting
new ContrastFilter(), // Enhance contrast
new SharpenFilter() // Improve edge definition
}
};
// Read with advanced configuration
BarcodeResults result = BarcodeReader.Read("QR.png", options);
Imports IronBarCode
Imports System
Imports System.Linq
' Configure advanced reading options
Private options As New BarcodeReaderOptions With {
.Speed = ReadingSpeed.Faster,
.ExpectMultipleBarcodes = False,
.ExpectBarcodeTypes = BarcodeEncoding.QRCode,
.Multithreaded = True,
.MaxParallelThreads = 4,
.RemoveFalsePositive = True,
.ImageFilters = New ImageFilterCollection() From {
New AdaptiveThresholdFilter(),
New ContrastFilter(),
New SharpenFilter()
}
}
' Read with advanced configuration
Private result As BarcodeResults = BarcodeReader.Read("QR.png", options)
這些進階讀取選項可在光線不足、影像變形或列印品質不佳等嚴苛條件下,仍能可靠地偵測 QR 碼。
QR 碼的發展下一步是什麼?
既然您已掌握使用 IronBarcode 生成 QR 碼的方法,不妨進一步探索以下進階主題:
下載資源
請參閱完整的原始碼與範例:
API 文件
請參閱 API 參考文件以了解完整功能集:
替代方案:IronQR 適用於進階 QR 應用
若您的專案需要尖端的 QR 碼功能,請考慮 IronQR——這是 Iron Software 專為 QR 碼設計的程式庫,具備由機器學習驅動的 99.99% 讀取準確度,以及進階的生成選項。
準備好在您的 .NET 應用程式中實作 QR 碼了嗎? 立即開始免費試用,或下載 IronBarcode。
常見問題
如何在C#中生成QR碼?
您可以使用IronBarcode的QRCodeWriter.CreateQrCode()方法在C#中生成QR碼。這個方法允許您傳遞內容、大小和錯誤校正級別以高效創建QR碼。
QR碼可以匯出為哪些圖片格式?
使用IronBarcode,您可以將QR碼匯出為多種格式,包括PNG、JPEG、PDF和HTML。使用SaveAsPng()、SaveAsJpeg()、SaveAsPdf()、和SaveAsHtmlFile()等方法實現。
如何將公司標誌添加到QR碼上?
IronBarcode提供CreateQrCodeWithLogo()方法,您可以傳遞包含標誌圖片的QRCodeLogo物件。程式庫將確保標誌大小和位置正確,從而保持QR碼的可讀性。
什麼是QR碼的錯誤校正,應選擇哪個級別?
QR碼中的錯誤校正使它們即使部分損壞也能被掃描。IronBarcode提供四個級別:低(7%)、中(15%)、四分(25%)和高(30%)。中級別適合大多數用途,而高級別則適合苛刻環境。
如何驗證自訂QR碼的可讀性?
您可以在GeneratedBarcode物件上使用Verify()方法,以確保您的自訂QR碼在如顏色變更或添加標誌之類的修改後仍然可掃描。
QR碼中可以編碼二進位資料嗎?
是的,IronBarcode的CreateQrCode()方法支持編碼位元組數組,允許您在QR碼中儲存如檔案或加密內容等二進位資料。
如何從圖片中讀取QR碼於C#中?
要在C#中從圖片讀取QR碼,使用IronBarcode的BarcodeReader.Read()方法。為了最佳化性能,在BarcodeReaderOptions中指定BarcodeEncoding.QRCode。
QR碼的最大數據容量是多少?
IronBarcode生成的QR碼最多可以容納2,953個位元組、4,296個字母數字字符或7,089個數字字符,具體取決於所選的錯誤校正級別。
如何更改QR碼的顏色且確保其仍然可掃描?
IronBarcode中的ChangeBarCodeColor()方法允許您更改QR碼的顏色。總是用Verify()方法在後面確認顏色變更不影響QR碼的可讀性。
專用QR碼程式庫提供了哪些功能?
IronQR,Iron Software的一個專用程式庫,包含高級功能,如機器學習驅動的QR碼讀取,具有99.99%的準確率及針對複雜應用的強大生成能力。

