Generate QR Codes in C# - Complete Tutorial for .NET Developers
需要在您的C#應用程式中生成QR碼嗎? 本教程精確地展示如何使用IronBarcode來建立、自訂和驗證QR碼——從簡單的一行實作到嵌入標誌和二進位資料編碼等進階功能。
無論您是在建構庫存系統、活動票務平台還是非接觸式支付解決方案,您都將學習如何在您的.NET應用程式中實現專業級的QR碼功能。
快速入門:使用IronBarcode的一行QR碼建立
準備好快速生成QR碼了嗎?以下是使用IronBarcode的QRCodeWriterAPI通過一行程式碼生成QR碼的方法——自訂雖然可選但功能強大。
最小化工作流程(5步驟)
- Install IronBarcode via NuGet
- 用一行程式碼建立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碼生成功能:
:path=/static-assets/barcode/content-code-examples/tutorials/csharp-qr-code-generator-3.cs
using IronBarCode;
// You may add styling with color, logo images or branding:
QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png");
GeneratedBarcode myQRCodeWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo);
myQRCodeWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkGreen);
// Save as PDF
myQRCodeWithLogo.SaveAsPdf("MyQRWithLogo.pdf");
// Also Save as HTML
myQRCodeWithLogo.SaveAsHtmlFile("MyQRWithLogo.html");
Imports IronBarCode
' You may add styling with color, logo images or branding:
Private qrCodeLogo As New QRCodeLogo("visual-studio-logo.png")
Private myQRCodeWithLogo As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo)
myQRCodeWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkGreen)
' Save as PDF
myQRCodeWithLogo.SaveAsPdf("MyQRWithLogo.pdf")
' Also Save as HTML
myQRCodeWithLogo.SaveAsHtmlFile("MyQRWithLogo.html")
How Can I Create a Simple QR Code in C#?
使用CreateQrCode方法僅用一行程式碼生成QR碼:
:path=/static-assets/barcode/content-code-examples/tutorials/csharp-qr-code-generator-4.cs
using IronBarCode;
using IronSoftware.Drawing;
using System;
// Verifying QR Codes
QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png");
GeneratedBarcode MyVerifiedQR = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo);
MyVerifiedQR.ChangeBarCodeColor(System.Drawing.Color.LightBlue);
if (!MyVerifiedQR.Verify())
{
Console.WriteLine("\t LightBlue is not dark enough to be read accurately. Lets try DarkBlue");
MyVerifiedQR.ChangeBarCodeColor(Color.DarkBlue);
}
MyVerifiedQR.SaveAsHtmlFile("MyVerifiedQR.html");
// open the barcode html file in your default web browser
System.Diagnostics.Process.Start("MyVerifiedQR.html");
Imports Microsoft.VisualBasic
Imports IronBarCode
Imports IronSoftware.Drawing
Imports System
' Verifying QR Codes
Private qrCodeLogo As New QRCodeLogo("visual-studio-logo.png")
Private MyVerifiedQR As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo)
MyVerifiedQR.ChangeBarCodeColor(System.Drawing.Color.LightBlue)
If Not MyVerifiedQR.Verify() Then
Console.WriteLine(vbTab & " LightBlue is not dark enough to be read accurately. Lets try DarkBlue")
MyVerifiedQR.ChangeBarCodeColor(Color.DarkBlue)
End If
MyVerifiedQR.SaveAsHtmlFile("MyVerifiedQR.html")
' open the barcode html file in your default web browser
System.Diagnostics.Process.Start("MyVerifiedQR.html")
CreateQrCode方法接受三個參數:
- 文字內容:要編碼的資料(支持URLs、文字或任意字串資料)
- 大小:方形QR碼的像素尺寸(本範例為500x500)
- 錯誤糾正:決定在次優化條件下的可讀性(低、中等、Quartile或高)
更高的錯誤糾正級別,即使在部分損壞或被遮擋的情況下,QR碼仍能保持可讀性,但會導致更密集的圖案和更多的資料模塊。
包含'hello world'文字的基本QR碼,生成於500x500像素並具有中等錯誤糾正
如何將標誌新增到我的QR碼中?
在QR碼中嵌入標誌可增強品牌識別,同時保持掃描性。 IronBarcode會自動定位並調整標誌大小以保證QR碼的完整性:
:path=/static-assets/barcode/content-code-examples/tutorials/csharp-qr-code-generator-5.cs
using IronBarCode;
using System;
using System.Linq;
// Create Some Binary Data - This example equally well for Byte[] and System.IO.Stream
byte[] BinaryData = System.Text.Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/barcode/");
// WRITE QR with Binary Content
QRCodeWriter.CreateQrCode(BinaryData, 500).SaveAsImage("MyBinaryQR.png");
// READ QR with Binary Content
var MyReturnedData = BarcodeReader.Read("MyBinaryQR.png").First();
if (BinaryData.SequenceEqual(MyReturnedData.BinaryValue))
{
Console.WriteLine("\t Binary Data Read and Written Perfectly");
}
else
{
throw new Exception("Corrupted Data");
}
Imports Microsoft.VisualBasic
Imports IronBarCode
Imports System
Imports System.Linq
' Create Some Binary Data - This example equally well for Byte[] and System.IO.Stream
Private BinaryData() As Byte = System.Text.Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/barcode/")
' WRITE QR with Binary Content
QRCodeWriter.CreateQrCode(BinaryData, 500).SaveAsImage("MyBinaryQR.png")
' READ QR with Binary Content
Dim MyReturnedData = BarcodeReader.Read("MyBinaryQR.png").First()
If BinaryData.SequenceEqual(MyReturnedData.BinaryValue) Then
Console.WriteLine(vbTab & " Binary Data Read and Written Perfectly")
Else
Throw New Exception("Corrupted Data")
End If
CreateQrCodeWithLogo方法智能地處理標誌的放置:
- 自動調整標誌大小以保持QR碼的可讀性
- 將其定位在安靜區域內以避免資料損壞
- 在更改QR碼顏色時保留標誌的原始顏色
這種方法確保您的品牌QR碼在所有掃描裝置和應用中保持完全功能。
展示IronBarcode的自動標誌尺寸調整和定位的QR碼,具有Visual Studio標誌
如何將QR碼匯出為不同格式?
IronBarcode支持多種輸出格式以應對不同的使用情況。 將您的QR碼匯出為圖片、PDF或HTML文件:
:path=/static-assets/barcode/content-code-examples/tutorials/csharp-qr-code-generator-6.cs
using IronBarCode;
using System;
using System.Linq;
BarcodeResults result = BarcodeReader.Read("QR.png", new BarcodeReaderOptions() { ExpectBarcodeTypes = BarcodeEncoding.QRCode });
if (result != null)
{
Console.WriteLine(result.First().Value);
}
Imports IronBarCode
Imports System
Imports System.Linq
Private result As BarcodeResults = BarcodeReader.Read("QR.png", New BarcodeReaderOptions() With {.ExpectBarcodeTypes = BarcodeEncoding.QRCode})
If result IsNot Nothing Then
Console.WriteLine(result.First().Value)
End If
每種格式擁有其特定用途:
- PDF:理想用於可列印文件和報告
- HTML:適合無需外部相依性的網頁整合
- PNG/JPEG:用於多種用途的標準圖像格式
如何在自訂後驗證QR碼的可讀性?
顏色修改和標誌新增可能影響QR碼的可掃描性。 使用Verify()方法確保您的自訂QR碼仍然可讀:
:path=/static-assets/barcode/content-code-examples/tutorials/csharp-qr-code-generator-7.cs
using IronBarCode;
using System;
using System.Linq;
BarcodeReaderOptions options = new BarcodeReaderOptions
{
Speed = ReadingSpeed.Faster,
ExpectMultipleBarcodes = false,
ExpectBarcodeTypes = BarcodeEncoding.All,
Multithreaded = false,
MaxParallelThreads = 0,
CropArea = null,
UseCode39ExtendedMode = false,
RemoveFalsePositive = false,
ImageFilters = null
};
BarcodeResults result = BarcodeReader.Read("QR.png", options);
if (result != null)
{
Console.WriteLine(result.First().Value);
}
Imports IronBarCode
Imports System
Imports System.Linq
Private options As New BarcodeReaderOptions With {
.Speed = ReadingSpeed.Faster,
.ExpectMultipleBarcodes = False,
.ExpectBarcodeTypes = BarcodeEncoding.All,
.Multithreaded = False,
.MaxParallelThreads = 0,
.CropArea = Nothing,
.UseCode39ExtendedMode = False,
.RemoveFalsePositive = False,
.ImageFilters = Nothing
}
Private result As BarcodeResults = BarcodeReader.Read("QR.png", options)
If result IsNot Nothing Then
Console.WriteLine(result.First().Value)
End If
Verify()方法對您的QR碼進行全面掃描測試。 這確保在不同的掃描裝置和光源條件下的相容性。
成功驗證的深藍色QR碼展示了可靠掃描的正確對比
如何在QR碼中編碼二進位資料?
QR碼在高效儲存二進位資料方面表現出色。 此功能使進階應用成為可能,例如資料加密傳輸、文件共享和IoT裝置配置:
:path=/static-assets/barcode/content-code-examples/tutorials/csharp-qr-code-generator-8.cs
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
Dim 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碼讀取功能。 這是最簡單的方法:
:path=/static-assets/barcode/content-code-examples/tutorials/csharp-qr-code-generator-9.cs
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
Dim 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
對於需要精細控制的更複雜情況:
:path=/static-assets/barcode/content-code-examples/tutorials/csharp-qr-code-generator-10.cs
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
Dim options As New BarcodeReaderOptions With {
.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() From { ' Apply preprocessing
New AdaptiveThresholdFilter(), ' Handle varying lighting
New ContrastFilter(), ' Enhance contrast
New SharpenFilter() ' Improve edge definition
}
}
' Read with advanced configuration
Dim 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#中?
要從圖像讀取QR碼到C#中,請使用IronBarcode的BarcodeReader.Read()方法。為優化性能,請在BarcodeReaderOptions中指定BarcodeEncoding.QRCode。
QR碼的最大資料容量是多少?
由IronBarcode生成的QR碼能容納最多2,953字節、4,296個字母數字字元或7,089個數字,具體取決於所選的錯誤修正等級。
如何更改QR碼的顏色並確保其仍然可掃描?
IronBarcode中的ChangeBarCodeColor()方法允許您更改QR碼的顏色。在更改顏色後,務必使用Verify()方法來確認QR碼的可讀性未受影響。
專門的QR碼程式庫提供哪些功能?
Iron Software的專門程式庫IronQR包括進階功能,如以機器學習驅動的QR碼閱讀,準確率高達99.99%,以及為複雜應用所定制的強大生成能力。

