如何在 .NET 6 中使用 QRCoder 與 IronQR 比較生成 QR 碼
在C#中生成QR碼
在C#應用程式中建立QR碼是開發人員的常見需求,尤其是對於涉及產品識別、票務或輕鬆分享URL和其他資料的應用程式。 有多個程式庫可用於在C#中生成QR碼。 有兩個值得注意的選擇是QRCoder和IronQR。 在這裡,我們將探討兩者,比較它們的功能、易用性、性能和其他與使用.NET 6的開發人員相關的因素。在本文中,我們將深入比較這些程式庫,檢視它們的功能、易用性、授權等,並附上程式碼範例來說明它們的使用。
如何使用QRCoder與IronQR在.NET 6中生成QR碼
- 建立一個新的Visual Studio專案以生成QR碼
- 安裝QRCoder和IronQR NuGet套件以進行比較
- 使用QRCoder和IronQR建立QR碼
- 使用QRCoder和IronQR的自定義選項
QRCoder
QRCoder是一個用C#編寫的開源程式庫,提供QR碼實現,允許您在任何.NET應用程式中生成按ISO/IEC 18004定義的QR碼。 這是一個輕量且易於使用的程式庫,無需依賴其他程式庫或網路支援。
以下是QRCoder的關鍵特性和優勢:
1. 易用性
C# QRCoder提供了一個簡單直觀的API,使開發人員能以最小的努力生成QR碼/QR碼文字。 其簡單性允許各種技能水平的開發人員快速將QR碼生成整合到他們的專案中。
2. 廣泛的自定義選項
QRCoder的一個突出特點是能根據特定需求自定義QR碼。 開發人員可以調整如錯誤更正級別、大小、顏色,甚至嵌入logo或圖片到QR碼中。
3. 多種編碼格式
QRCoder支援各種型別的編碼格式,使開發者能將不同型別的資料編碼到QR碼中。 無論是純文字、URL、聯絡資訊還是Wi-Fi憑證,QRCoder都能處理各類資料格式。
4. 高品質輸出
C# QRCoder生成的QR碼具有高質量,確保其在不同裝置和掃描條件下的可讀性和可靠性。 這種可靠性對於將QR碼作為物理和數字交互橋樑的應用程式至關重要。
5. 開源和積極開發
C# QRCoder是一個開源專案,允許開發者對其進行貢獻,確保持續的改進和更新。 這種積極的社群參與促進了創新,確保該程式庫在技術不斷演變的景觀中保持相關性。
IronQR
IronQR是一個由Iron Software開發和維護的強大C# QR碼程式庫。 它允許C#軟體工程師在.NET應用程式和網站中檢測、讀取和建立QR碼。 以下是IronQR的一些主要特點:
QR碼生成
IronQR支持高度自定義的QR碼生成。 您可以使用各種選項建立QR碼,如調整大小、邊距、邊框和重新上色。
using IronQr;
using IronSoftware.Drawing;
public class Program
{
public static void Main()
{
// Prepare a QR Code object
QrCode theQrGen = QrWriter.Write("Awesome IronQR");
// Save QR Code to memory
AnyBitmap myQrImage = theQrGen.Save();
// Save QR Code image to disk
myQrImage.SaveAs("awesome.png");
}
}
using IronQr;
using IronSoftware.Drawing;
public class Program
{
public static void Main()
{
// Prepare a QR Code object
QrCode theQrGen = QrWriter.Write("Awesome IronQR");
// Save QR Code to memory
AnyBitmap myQrImage = theQrGen.Save();
// Save QR Code image to disk
myQrImage.SaveAs("awesome.png");
}
}
Imports IronQr
Imports IronSoftware.Drawing
Public Class Program
Public Shared Sub Main()
' Prepare a QR Code object
Dim theQrGen As QrCode = QrWriter.Write("Awesome IronQR")
' Save QR Code to memory
Dim myQrImage As AnyBitmap = theQrGen.Save()
' Save QR Code image to disk
myQrImage.SaveAs("awesome.png")
End Sub
End Class
QR碼讀取
IronQR使用先進的機器學習模型進行QR碼檢測。 該模型確保準確和快速的QR碼讀取。 IronQR支援從各種圖片格式(包括JPG、PNG、SVG、bmp)以及多頁圖片(如gif和tiff)中讀取QR碼。
using IronQr;
using IronSoftware.Drawing;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
// Read QR code
var inputBmp = AnyBitmap.FromFile("awesome.png");
// Load the image into QrImageInput
QrImageInput imageInput = new QrImageInput(inputBmp);
// Create the QR Reader object
QrReader reader = new QrReader();
// Read the Input and get all embedded QR Codes
IEnumerable<QrResult> results = reader.Read(imageInput);
}
}
using IronQr;
using IronSoftware.Drawing;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
// Read QR code
var inputBmp = AnyBitmap.FromFile("awesome.png");
// Load the image into QrImageInput
QrImageInput imageInput = new QrImageInput(inputBmp);
// Create the QR Reader object
QrReader reader = new QrReader();
// Read the Input and get all embedded QR Codes
IEnumerable<QrResult> results = reader.Read(imageInput);
}
}
Imports IronQr
Imports IronSoftware.Drawing
Imports System.Collections.Generic
Public Class Program
Public Shared Sub Main()
' Read QR code
Dim inputBmp = AnyBitmap.FromFile("awesome.png")
' Load the image into QrImageInput
Dim imageInput As New QrImageInput(inputBmp)
' Create the QR Reader object
Dim reader As New QrReader()
' Read the Input and get all embedded QR Codes
Dim results As IEnumerable(Of QrResult) = reader.Read(imageInput)
End Sub
End Class
跨平台相容性
- 桌面:適用於Windows桌面應用程式(WPF & MAUI)。
- 移動端:相容於Xamarin和MAUI。
- 網路:支援Blazor和WebForms。
- 控制台:應用程式和程式庫環境。
- 雲端:Docker、Azure和AWS。
編碼資料
您可以在QR碼中編碼各種型別的資料,包括文字、URL、位元組和數字。
錯誤更正
IronQR提供詳細的錯誤資訊和自定義錯誤更正選項。
受到數百萬人的信任,IronQR因其可靠性和易用性而受全球工程師青睞。 要開始使用IronQR,您可以通過NuGet安裝它。
步驟1:建立一個新的Visual Studio專案以生成QR碼
要開始程式碼工作,我們先建立一個Visual Studio專案。 打開Microsoft Visual Studio 2022,選擇"建立新專案"選項。

從模板列表中選擇控制台應用程式模板

然後提供專案名稱和解決方案名稱。 選擇儲存專案文件的路徑

選擇需要的.NET版本。 在撰寫本文時,我將選擇最新的.NET 8框架。

一旦你點擊建立按鈕,專案就會被建立並準備好開發。
步驟2:安裝QRCoder和IronQR NuGet套件
QRCoder NuGet套件可以使用Visual Studio NuGet套件管理器安裝,如下所示。

或從NuGet套件管理器控制台使用以下命令安裝它
dotnet add package QRCoder --version 1.4.3
QRCoder NuGet套件在這裡的NuGet網站上可用

IronQR也可以通過類似方式使用Visual Studio NuGet套件管理器安裝,如下所示。

也可以從NuGet套件管理器控制台使用以下命令
dotnet add package IronQR --version 2024.4.1

步驟3:使用QRCoder和IronQR建立QR碼
在兩個程式庫中建立QR碼需要簡單的程式碼,讓我們來看看它們
QRCoder
以下程式碼顯示如何使用QRCoder生成我們的第一次QR碼
using QRCoder;
using System.Drawing;
using System.IO;
namespace QRCoderVsIronQR
{
public class Program
{
public static void Main()
{
// Initialize QRCodeGenerator
using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
// Create QR code data
using (QRCodeData qrCodeData = qrGenerator.CreateQrCode("QRCoder Demo 1", QRCodeGenerator.ECCLevel.Q))
// Initialize the QR code with the data
using (BitmapByteQRCode qrCode = new BitmapByteQRCode(qrCodeData))
{
// Generate the QR code's graphic and store it in a byte array
byte[] qrCodeImage = qrCode.GetGraphic(20);
// Convert the byte array to an image format and save it to disk
using (var file = Image.FromStream(new MemoryStream(qrCodeImage)))
{
file.Save("QrCoderDemo1.png");
}
}
}
}
}
using QRCoder;
using System.Drawing;
using System.IO;
namespace QRCoderVsIronQR
{
public class Program
{
public static void Main()
{
// Initialize QRCodeGenerator
using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
// Create QR code data
using (QRCodeData qrCodeData = qrGenerator.CreateQrCode("QRCoder Demo 1", QRCodeGenerator.ECCLevel.Q))
// Initialize the QR code with the data
using (BitmapByteQRCode qrCode = new BitmapByteQRCode(qrCodeData))
{
// Generate the QR code's graphic and store it in a byte array
byte[] qrCodeImage = qrCode.GetGraphic(20);
// Convert the byte array to an image format and save it to disk
using (var file = Image.FromStream(new MemoryStream(qrCodeImage)))
{
file.Save("QrCoderDemo1.png");
}
}
}
}
}
Imports QRCoder
Imports System.Drawing
Imports System.IO
Namespace QRCoderVsIronQR
Public Class Program
Public Shared Sub Main()
' Initialize QRCodeGenerator
Using qrGenerator As New QRCodeGenerator()
' Create QR code data
Using qrCodeData As QRCodeData = qrGenerator.CreateQrCode("QRCoder Demo 1", QRCodeGenerator.ECCLevel.Q)
' Initialize the QR code with the data
Using qrCode As New BitmapByteQRCode(qrCodeData)
' Generate the QR code's graphic and store it in a byte array
Dim qrCodeImage() As Byte = qrCode.GetGraphic(20)
' Convert the byte array to an image format and save it to disk
Using file = Image.FromStream(New MemoryStream(qrCodeImage))
file.Save("QrCoderDemo1.png")
End Using
End Using
End Using
End Using
End Sub
End Class
End Namespace
程式碼說明
- 初始化
CreateQrCode方法來生成QR碼資料。 - 使用
GetGraphic方法。 - 使用
Image.FromStream將位原陣列轉換為圖片。 - 將生成的圖片保存到磁碟上。
輸出

IronQR
以下程式碼顯示如何使用IronQR生成QR碼:
using IronQr;
using IronSoftware.Drawing;
namespace QrCodeWithIronQR
{
public class Program
{
public static void Main()
{
// Prepare a QR Code object
QrCode theQrGen = QrWriter.Write("Awesome IronQR");
// Save QR Code to memory
AnyBitmap myQrImage = theQrGen.Save();
// Save QR Code image to disk
myQrImage.SaveAs("awesome.png");
}
}
}
using IronQr;
using IronSoftware.Drawing;
namespace QrCodeWithIronQR
{
public class Program
{
public static void Main()
{
// Prepare a QR Code object
QrCode theQrGen = QrWriter.Write("Awesome IronQR");
// Save QR Code to memory
AnyBitmap myQrImage = theQrGen.Save();
// Save QR Code image to disk
myQrImage.SaveAs("awesome.png");
}
}
}
Imports IronQr
Imports IronSoftware.Drawing
Namespace QrCodeWithIronQR
Public Class Program
Public Shared Sub Main()
' Prepare a QR Code object
Dim theQrGen As QrCode = QrWriter.Write("Awesome IronQR")
' Save QR Code to memory
Dim myQrImage As AnyBitmap = theQrGen.Save()
' Save QR Code image to disk
myQrImage.SaveAs("awesome.png")
End Sub
End Class
End Namespace
正如您已經看到的,程式碼量的差異。 IronQR程式碼生成需要更少的程式碼。
程式碼說明
- 使用
QrWriter.Write方法建立QR碼,傳遞所需內容。 - 使用
Save方法將QR碼保存到記憶體。 - 使用
SaveAs將QR碼圖片保存到磁碟上。
輸出
由於我使用的是該程式庫的試用版,因此輸出有IronQR水印。 使用許可版本,這將被移除。

步驟4:使用QRCoder和IronQR的自定義選項
這兩個程式庫都支援自定義選項。 讓我們看看一些可用的選項
我們可以在QRCoder中設置QR碼顏色,如下所示:
using QRCoder;
using System.Drawing;
using System.IO;
namespace QRCoderVsIronQR
{
public class Program
{
public static void Main()
{
// Initialize QRCodeGenerator
using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
// Create QR code data
using (QRCodeData qrCodeData = qrGenerator.CreateQrCode("QRCoder Demo 1", QRCodeGenerator.ECCLevel.Q))
// Initialize the QR code with the data
using (BitmapByteQRCode qrCode = new BitmapByteQRCode(qrCodeData))
{
// Generate the QR code's graphic, specifying foreground and background colors
byte[] qrCodeImage = qrCode.GetGraphic(20, Color.Red, Color.Green);
// Convert the byte array to an image format and save it to disk
using (var file = Image.FromStream(new MemoryStream(qrCodeImage)))
{
file.Save("QrCoderDemo1.png");
}
}
}
}
}
using QRCoder;
using System.Drawing;
using System.IO;
namespace QRCoderVsIronQR
{
public class Program
{
public static void Main()
{
// Initialize QRCodeGenerator
using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
// Create QR code data
using (QRCodeData qrCodeData = qrGenerator.CreateQrCode("QRCoder Demo 1", QRCodeGenerator.ECCLevel.Q))
// Initialize the QR code with the data
using (BitmapByteQRCode qrCode = new BitmapByteQRCode(qrCodeData))
{
// Generate the QR code's graphic, specifying foreground and background colors
byte[] qrCodeImage = qrCode.GetGraphic(20, Color.Red, Color.Green);
// Convert the byte array to an image format and save it to disk
using (var file = Image.FromStream(new MemoryStream(qrCodeImage)))
{
file.Save("QrCoderDemo1.png");
}
}
}
}
}
Imports QRCoder
Imports System.Drawing
Imports System.IO
Namespace QRCoderVsIronQR
Public Class Program
Public Shared Sub Main()
' Initialize QRCodeGenerator
Using qrGenerator As New QRCodeGenerator()
' Create QR code data
Using qrCodeData As QRCodeData = qrGenerator.CreateQrCode("QRCoder Demo 1", QRCodeGenerator.ECCLevel.Q)
' Initialize the QR code with the data
Using qrCode As New BitmapByteQRCode(qrCodeData)
' Generate the QR code's graphic, specifying foreground and background colors
Dim qrCodeImage() As Byte = qrCode.GetGraphic(20, Color.Red, Color.Green)
' Convert the byte array to an image format and save it to disk
Using file = Image.FromStream(New MemoryStream(qrCodeImage))
file.Save("QrCoderDemo1.png")
End Using
End Using
End Using
End Using
End Sub
End Class
End Namespace
程式碼說明
- 初始化
CreateQrCode方法來生成QR碼資料。 - 使用
GetGraphic方法,指定前景(紅色)和背景(綠色)顏色。 - 使用
Image.FromStream將位原陣列轉換為圖片。 - 將生成的圖片保存到磁碟上。
輸出

我們可以像下面的程式碼設置IronQR QR碼生成顏色:
using IronQr;
using IronSoftware.Drawing;
using System.Drawing;
namespace QrCodeWithIronQR
{
public class Program
{
public static void Main()
{
// Set options
QrOptions options = new QrOptions(QrErrorCorrectionLevel.Medium, 20);
// Create QR
QrCode myQr = QrWriter.Write("IronQR Generation Demo 1", options);
// Style options
AnyBitmap logoBmp = new AnyBitmap("logo.png");
QrStyleOptions style = new QrStyleOptions
{
BackgroundColor = Color.Aqua,
Dimensions = 300, // px
Margins = 10, // px
Color = Color.Red,
Logo = new QrLogo
{
Bitmap = logoBmp,
Width = 100,
Height = 100,
CornerRadius = 2
}
};
// Save QR Code
AnyBitmap qrImage = myQr.Save(style);
// Save QR Code to local disk
qrImage.SaveAs("advancedQr.png");
}
}
}
using IronQr;
using IronSoftware.Drawing;
using System.Drawing;
namespace QrCodeWithIronQR
{
public class Program
{
public static void Main()
{
// Set options
QrOptions options = new QrOptions(QrErrorCorrectionLevel.Medium, 20);
// Create QR
QrCode myQr = QrWriter.Write("IronQR Generation Demo 1", options);
// Style options
AnyBitmap logoBmp = new AnyBitmap("logo.png");
QrStyleOptions style = new QrStyleOptions
{
BackgroundColor = Color.Aqua,
Dimensions = 300, // px
Margins = 10, // px
Color = Color.Red,
Logo = new QrLogo
{
Bitmap = logoBmp,
Width = 100,
Height = 100,
CornerRadius = 2
}
};
// Save QR Code
AnyBitmap qrImage = myQr.Save(style);
// Save QR Code to local disk
qrImage.SaveAs("advancedQr.png");
}
}
}
Imports IronQr
Imports IronSoftware.Drawing
Imports System.Drawing
Namespace QrCodeWithIronQR
Public Class Program
Public Shared Sub Main()
' Set options
Dim options As New QrOptions(QrErrorCorrectionLevel.Medium, 20)
' Create QR
Dim myQr As QrCode = QrWriter.Write("IronQR Generation Demo 1", options)
' Style options
Dim logoBmp As New AnyBitmap("logo.png")
Dim style As New QrStyleOptions With {
.BackgroundColor = Color.Aqua,
.Dimensions = 300,
.Margins = 10,
.Color = Color.Red,
.Logo = New QrLogo With {
.Bitmap = logoBmp,
.Width = 100,
.Height = 100,
.CornerRadius = 2
}
}
' Save QR Code
Dim qrImage As AnyBitmap = myQr.Save(style)
' Save QR Code to local disk
qrImage.SaveAs("advancedQr.png")
End Sub
End Class
End Namespace
使用IronQR有許多自定義選項。 在上述程式中,我們嘗試自定義背景色、QR碼顏色、邊距。 我們還可以設置尺寸。
程式碼說明
- 首先,建立
QrOptions物件以設置錯誤更正級別。 - 用
QrWriter.Write方法準備QR碼。 - 用
QrStyleOptions設置樣式選項。 - 設置背景色、前景色、邊距、尺寸,甚至QR碼的logo。
- 將QR碼圖片保存到本地磁碟。
輸出

授權
QRCoder是一個基於MIT授權的套件,並在社群的幫助下開發。 此套件適合小型預算專案。 使用者需要等待社群解決問題,或可以修正並推送程式碼到Git倉庫,待pull request批准後。
IronQR需要授權。 它得到Iron Software的支持。
可以獲得IronQR試用授權。 需將獲得的金鑰放在此處的appSettings.json文件中:
{
"IronQr.License.LicenseKey": "myKey"
}
結論
選擇C# QRCoder和IronQR最終取決於您專案的具體需求。 如果您需要一個免費且開源的QR碼生成程式庫,有大量的自定義選項,那麼由於其簡單性和廣泛的自定義選項,C# QRCoder可能是首選。
如果您需要一個企業級程式庫,不僅支持帶自定義選項的QR碼生成,還支持QR碼讀取,那麼IronQR將是最終選擇。使用IronQR程式庫,其大量的優勢以及來自Iron Software的支援功能,開發者可以毫不費力地編寫企業應用程式,並獲得安心的使用體驗。
常見問題
如何在C#中生成QR Code?
您可以使用像QRCoder和IronQR這樣的程式庫在C#中生成QR Code。QRCoder是開源的,提供大量的自訂選項,而IronQR提供進階功能及跨平台支援,非常適合企業級應用。
在.NET 6專案中使用QRCoder有哪些好處?
QRCoder對於.NET 6專案有益,因為它易於使用,有大量的自訂選項,且支援多種編碼格式。由於其基於MIT許可證免費使用,因此適合預算有限的專案。
如何使用IronQR自訂QR Code?
IronQR允許開發者透過調整背景色、QR Code顏色、邊距、尺寸以至於加入標誌等參數來自訂QR Code以改善其外觀。
IronQR對跨平台應用程式有什麼優勢?
IronQR支援跨平台應用,包括桌面(WPF和MAUI)、移動(Xamarin和MAUI)、網頁(Blazor和WebForms)、控制台應用,還有像Docker、Azure和AWS這樣的雲端環境,使其非常適合各種開發需求。
在Visual Studio中設立QR Code生成專案需要哪些步驟?
要在Visual Studio中設立QR Code生成專案,您需要根據選擇安裝QRCoder或IronQR的所需NuGet套件。文章提供了逐步指導和程式碼範例來指導您完成整個過程。
IronQR如何增強QR Code的錯誤修正?
IronQR透過提供詳細選項來增強QR Code的錯誤修正,以確保即使QR Code受損或部分被遮擋仍能準確讀取。這使其對於關鍵應用非常可靠。
IronQR有哪些許可選項?
IronQR需要商業授權,由Iron Software提供支援,並可選擇取得試用授權進行評估。這確保企業級專案能獲得強大的支援和功能。
對於簡單的QR Code專案,我應如何選擇程式庫?
對於簡單的QR Code專案,推薦使用QRCoder,因為它簡單易用且具成本效益,是基於MIT許可證的開源程式庫。
IronQR能讀取各種圖像格式的QR Code嗎?
可以,IronQR可以從多種圖像格式讀取QR Code,使用先進的機器學習模型,確保在不同平台上快速準確地讀取。




