用 C# 寫二維碼

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

介紹

借助 IronQR,開發人員可以為流行的圖像格式建立二維碼,並使用背景顏色、邊距、徽標對其進行自訂,甚至可以將其添加到 PDF 中。 對於高級用戶,它還提供錯誤修正和版本控制功能。

本文將透過範例探討 IronQR 的關鍵特性,幫助您了解如何在 C# 中使用它來編寫二維碼,並在您的專案中有效地應用它。

目錄

輸入資料 文字、網址、數字 -二進制和流 匯出二維碼 -另存為影像

立即開始在您的項目中使用 IronQR 並免費試用。

第一步:
green arrow pointer

輸入資料

文字、網址、數字

IronQR 可以將包括文字、URL 和數字在內的各種資料類型轉換為二維碼。 無論您是建立用於行銷和溝通的二維碼連結或文本,用於庫存管理的數字代碼,還是將二進位資料或資料流編碼為可讀的二維碼,IronQR 都能提供您所需的一切支援。

此外,API 也非常簡單好用。 QrWriter類別提供了多個重載,支援不同類型的資料作為輸入,從而降低複雜性並簡化流程。

:path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-writing-1.cs
using IronQr;
using IronSoftware.Drawing;

string text = "Hello, World!";
string url = "https://ironsoftware.com/csharp/qr/";
string alphanumeric = "WATERSKU-12356";

// Create QR code
QrCode textQr = QrWriter.Write(text);
// Save QR code as a bitmap
AnyBitmap textQrImage = textQr.Save();
// Save QR code as file
textQrImage.SaveAs("textQr.png");

QrCode urlQr = QrWriter.Write(url);
AnyBitmap urlQrImage = urlQr.Save();
urlQrImage.SaveAs("urlQr.png");

QrCode alphanumericQr = QrWriter.Write(alphanumeric);
AnyBitmap alphanumericQrImage = alphanumericQr.Save();
alphanumericQrImage.SaveAs("alphanumericQr.png");
Imports IronQr
Imports IronSoftware.Drawing

Private text As String = "Hello, World!"
Private url As String = "https://ironsoftware.com/csharp/qr/"
Private alphanumeric As String = "WATERSKU-12356"

' Create QR code
Private textQr As QrCode = QrWriter.Write(text)
' Save QR code as a bitmap
Private textQrImage As AnyBitmap = textQr.Save()
' Save QR code as file
textQrImage.SaveAs("textQr.png")

Dim urlQr As QrCode = QrWriter.Write(url)
Dim urlQrImage As AnyBitmap = urlQr.Save()
urlQrImage.SaveAs("urlQr.png")

Dim alphanumericQr As QrCode = QrWriter.Write(alphanumeric)
Dim alphanumericQrImage As AnyBitmap = alphanumericQr.Save()
alphanumericQrImage.SaveAs("alphanumericQr.png")
$vbLabelText   $csharpLabel

二進制和流

類似地,我們可以使用前面提到的相同Write方法將二進位資料和資料流轉換為 QR 碼。

:path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-writing-2.cs
using IronQr;
using IronSoftware.Drawing;
using System.Text;

byte[] bytes = Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/qr/");

// Create QR code
QrCode bytesQr = QrWriter.Write(bytes);

// Save QR code as a bitmap
AnyBitmap qrImage = bytesQr.Save();

// Save QR code bitmap to file
qrImage.SaveAs("bytesQr.png");
Imports IronQr
Imports IronSoftware.Drawing
Imports System.Text

Private bytes() As Byte = Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/qr/")

' Create QR code
Private bytesQr As QrCode = QrWriter.Write(bytes)

' Save QR code as a bitmap
Private qrImage As AnyBitmap = bytesQr.Save()

' Save QR code bitmap to file
qrImage.SaveAs("bytesQr.png")
$vbLabelText   $csharpLabel

課程項目 { static void Main() { // 建立二維碼寫入器實例 QrWriter writer = QrWriter.CreateQrCode();

    // 範例二進位數據
    byte[] data = { 0x01, 0x02, 0x03, 0x04 };

    // 將二進位資料寫入二維碼
    writer.Write(data)
          .SaveAs("binary-qr.png");

    // 使用記憶體流的範例
    使用 (MemoryStream stream = new MemoryStream(data))
    {
        writer.Write(stream)
              .SaveAs("stream-qr.png");
    }
}

}


`Write`方法有多個重載版本,可以接受位元組數組和流作為輸入。 對於流,我們可以從位元組數組建立`MemoryStream` ,然後將其轉換為二維碼。 當使用者需要對資料塊進行更精細的控制時,這非常有用,因為流可以更節省記憶體。

```cs
:path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-writing-3.cs

匯出二維碼

IronQR 具有靈活性和適應性,可滿足各種需要不同文件格式的使用情境。 您可以使用SaveAs方法將二維碼儲存為 JPG、PNG、GIF 和 TIFF 等多種格式。

另存為影像

AnyBitmapSaveAs方法會根據提供的檔案路徑自動偵測檔案格式。 在這個例子中,我指定了一個以.png結尾的檔案路徑。

請注意使用SaveAs方法時請注意,沒有預設的影像格式。 如果您輸入了無法辨識的檔案副檔名或檔案路徑拼字錯誤,影像將以錯誤的副檔名儲存。

:path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-writing-4.cs
using IronQr;
using IronSoftware.Drawing;

// Create a QR code object
QrCode qr = QrWriter.Write("hello world");

// Save QR code as a bitmap
AnyBitmap qrImage = qr.Save();

// Save QR code bitmap as file
qrImage.SaveAs("qr.png");
Imports IronQr
Imports IronSoftware.Drawing

' Create a QR code object
Private qr As QrCode = QrWriter.Write("hello world")

' Save QR code as a bitmap
Private qrImage As AnyBitmap = qr.Save()

' Save QR code bitmap as file
qrImage.SaveAs("qr.png")
$vbLabelText   $csharpLabel

系統.繪圖.圖像

將映像轉換為 Microsoft 的System.Drawing.Images對象,即可使用Bitmap類別將二維碼儲存到檔案路徑。 在這個範例中, Save方法將二維碼儲存為 PNG 文件,並儲存到路徑qrBitmap.png

@@--括號-i-OPEN--@@ System.Drawing.Common僅在 Windows 平台上支援。 @@--括號-CLOSE--@@

:path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-writing-5.cs
using IronQr;
using System.Drawing;

// Create a QR code object
QrCode qr = QrWriter.Write("hello world");

// Save QR code as a bitmap
Bitmap qrImage = qr.Save();

// Save QR code bitmap as file
qrImage.Save("qrBitmap.png");
Imports IronQr
Imports System.Drawing

' Create a QR code object
Private qr As QrCode = QrWriter.Write("hello world")

' Save QR code as a bitmap
Private qrImage As Bitmap = qr.Save()

' Save QR code bitmap as file
qrImage.Save("qrBitmap.png")
$vbLabelText   $csharpLabel

IronSoftware.繪圖

由於System.Drawing.Common缺乏跨平台相容性,開發人員在維護跨平台應用程式時可能會遇到問題。 IronQR 可以同時利用System.Drawing.CommonIronSoftware.Drawing

IronQR 使用IronSoftware.Drawing中的AnyBitmap類,這是一個通用相容的 Bitmap 類,它會隱式轉換為以下類型:

  • System.Drawing.Bitmap
  • System.Drawing.Image
  • SkiaSharp.SKBitmap
  • SixLabors.ImageSharp
  • Microsoft.Maui.Graphics.Platform.PlatformImage

借助這個強大的開源程式庫,IronQR 實現了跨平台支持,並相容於 .NET 8、.NET 7、.NET 6、.NET 5、.NET Core、.NET Standard 和 .NET Framework 4.6.2+。 要了解有關該庫的更多信息,請訪問IronSoftware.Drawing網站。

在 PDF 上蓋章

IronQR 允許開發者在現有的 PDF 文件上新增二維碼,方便其他人快速存取連結或其他資源。 支援在單頁和多頁上新增二維碼。

單頁蓋章

建立二維碼後,從QrCode物件呼叫StampToExistingPdfPage方法。 此方法需要檔案路徑、座標(x 和 y)、頁碼,如果 PDF 受密碼保護,則還需要密碼(可選)。 提供參數後,方法會產生二維碼並儲存 PDF 檔案。

此方法基於 PDF 頁碼,頁碼從 1 開始,而不是從 0 開始。

:path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-writing-6.cs
using IronQr;

// Create a QR code object
QrCode qr = QrWriter.Write("hello world");

string filepath = "example.pdf";
int x = 100;
int y = 150;
int page = 1;

// Stamp QR code to (100, 150) of the pdf on page 1
qr.StampToExistingPdfPage(filepath, x, y, page);
Imports IronQr

' Create a QR code object
Private qr As QrCode = QrWriter.Write("hello world")

Private filepath As String = "example.pdf"
Private x As Integer = 100
Private y As Integer = 150
Private page As Integer = 1

' Stamp QR code to (100, 150) of the pdf on page 1
qr.StampToExistingPdfPage(filepath, x, y, page)
$vbLabelText   $csharpLabel

在多頁上蓋章

與上面的例子類似,主要區別在於StampToExistingPdfPages方法接受的是頁碼列表而不是單一頁碼。

:path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-writing-7.cs
using IronQr;
using System.Collections.Generic;

// Create a QR code object
QrCode qr = QrWriter.Write("hello world");

string filepath = "example.pdf";
int x = 100;
int y = 150;
List<int> pages = new List<int>();
pages.Add(1);
pages.Add(2);
pages.Add(3);
pages.Add(4);

// Stamp QR code to (100, 150) of the pdf on pages 1-4
qr.StampToExistingPdfPages(filepath, x, y, pages);
Imports IronQr
Imports System.Collections.Generic

' Create a QR code object
Private qr As QrCode = QrWriter.Write("hello world")

Private filepath As String = "example.pdf"
Private x As Integer = 100
Private y As Integer = 150
Private pages As New List(Of Integer)()
pages.Add(1)
pages.Add(2)
pages.Add(3)
pages.Add(4)

' Stamp QR code to (100, 150) of the pdf on pages 1-4
qr.StampToExistingPdfPages(filepath, x, y, pages)
$vbLabelText   $csharpLabel

兩個範例的輸出

alt text


QR 圖碼選項

IronQR 提供豐富的自訂選項,可對二維碼的行為和功能進行微調。 QrOptions類別提供了多個參數,例如版本控制、編碼類型、字元編碼和糾錯等級。 讓我們更詳細地探討一下這些選項。

編碼

IronQR 支援多種類型的二維碼,可用於建立和讀取。 以下是支援的類型:

  • QRCode :這是目前常用的標準二維碼。 它最多可以儲存 7,089 個數字字元或 4,296 個字母數字字元。
  • MicroQRCode :標準二維碼的縮小版,最多可儲存 35 個數字字元或 21 個字母數字字元。
  • RMQRCode :矩形微型二維碼是二維碼的緊湊版本,其長寬比具有靈活性。
:path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-writing-8.cs
using IronQr;
using IronSoftware.Drawing;

QrOptions options = new QrOptions
{
    // Change encoding to micro QR code
    Encoding = IronQr.Enum.QrEncoding.MicroQRCode,
};

// Create QR code
QrCode qr = QrWriter.Write("1234", options);

// Save QR code as a bitmap
AnyBitmap qrImage = qr.Save();

// Save QR code bitmap as file
qrImage.SaveAs("qrImage.png");
Imports IronQr
Imports IronSoftware.Drawing

Private options As New QrOptions With {.Encoding = IronQr.Enum.QrEncoding.MicroQRCode}

' Create QR code
Private qr As QrCode = QrWriter.Write("1234", options)

' Save QR code as a bitmap
Private qrImage As AnyBitmap = qr.Save()

' Save QR code bitmap as file
qrImage.SaveAs("qrImage.png")
$vbLabelText   $csharpLabel

錯誤糾正

IronQR 使用標準的 QR 糾錯技術,確保產生的所有 QR 碼都具有容錯性和可靠性,即使在惡劣條件下也是如此。 此外,IronQR 還允許您完全控制誤差修正級別,以便進行進一步的微調。

QrErrorCorrectionLevel提供了四個等級的糾錯機制:

Highest :30% 誤差校正

  • High :25% 誤差校正
  • Medium :15% 誤差校正
  • Low :7% 誤差校正
:path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-writing-9.cs
using IronQr;
using IronSoftware.Drawing;

QrOptions options = new QrOptions
{
    // Change error correction level to medium
    ErrorCorrectionLevel = QrErrorCorrectionLevel.Medium,
};

// Create QR code
QrCode qr = QrWriter.Write("1234", options);

// Save QR code as a bitmap
AnyBitmap qrImage = qr.Save();

// Save QR code bitmap as file
qrImage.SaveAs("qrImage.png");
Imports IronQr
Imports IronSoftware.Drawing

Private options As New QrOptions With {.ErrorCorrectionLevel = QrErrorCorrectionLevel.Medium}

' Create QR code
Private qr As QrCode = QrWriter.Write("1234", options)

' Save QR code as a bitmap
Private qrImage As AnyBitmap = qr.Save()

' Save QR code bitmap as file
qrImage.SaveAs("qrImage.png")
$vbLabelText   $csharpLabel

更高的糾錯能力在讀取二維碼時能提供更大的容錯能力,因此與糾錯能力低的二維碼相比,它更有可能在較低的解析度下被掃描。 您可以根據自己的使用場景進行測試。

alt text

QR 圖碼版本

您可以調整二維碼版本以儲存更多資料。 較高版本適用於庫存或物流,而較低版本適用於較小的數據,例如短連結。 只需更改QrOptions物件中的Version屬性,並將其傳遞給Write方法,即可根據需要產生二維碼。

:path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-writing-10.cs
using IronQr;
using IronSoftware.Drawing;

QrOptions options = new QrOptions
{
    // Change QR code version to 40
    Version = 40,
};

// Create QR code
QrCode qr = QrWriter.Write("1234", options);

// Save QR code as a bitmap
AnyBitmap qrImage = qr.Save();

// Save QR code bitmap as file
qrImage.SaveAs("qrImage.png");
Imports IronQr
Imports IronSoftware.Drawing

Private options As New QrOptions With {.Version = 40}

' Create QR code
Private qr As QrCode = QrWriter.Write("1234", options)

' Save QR code as a bitmap
Private qrImage As AnyBitmap = qr.Save()

' Save QR code bitmap as file
qrImage.SaveAs("qrImage.png")
$vbLabelText   $csharpLabel

alt text

從輸出結果可以看出,與版本 5 相比,版本 40 的二維碼非常複雜且密集。

低版本需要更精確的掃描,如果沒有高解析度掃描儀,可能難以掃描。 但是,即使使用解析度較低的相機,更高版本的檔案也更容易掃描。 有關根據容量選擇二維碼版本的更詳細指南,請參閱二維碼版本清單

字元編碼

此選項決定二維碼的編碼方式。 在我們的範例中,我們將其變更為"UTF-32",而預設字元編碼為"ISO-8859-1"。

:path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-writing-11.cs
using IronQr;
using IronSoftware.Drawing;

QrOptions options = new QrOptions
{
    // Change character encoding to UTF-32
    CharacterEncoding = "UTF-32"
};

// Create QR code
QrCode qr = QrWriter.Write("1234", options);

// Save QR code as a bitmap
AnyBitmap qrImage = qr.Save();

// Save QR code bitmap as file
qrImage.SaveAs("qrImage.png");
Imports IronQr
Imports IronSoftware.Drawing

Private options As New QrOptions With {.CharacterEncoding = "UTF-32"}

' Create QR code
Private qr As QrCode = QrWriter.Write("1234", options)

' Save QR code as a bitmap
Private qrImage As AnyBitmap = qr.Save()

' Save QR code bitmap as file
qrImage.SaveAs("qrImage.png")
$vbLabelText   $csharpLabel

QR 圖碼樣式

除了易於使用的方法和處理輸入資料的靈活性之外,IronQR 還提供了許多自訂和設定二維碼樣式的選項,使二維碼獨一無二。 QrStyleOptions類別提供了各種參數,用於自訂二維碼的各個方面。 讓我們來探討一下可行的方案。

調整大小

若要調整二維碼的大小,您可以設定QrStyleOptions物件的Dimensions屬性,然後將其傳遞給Save方法。 預設情況下,二維碼會儲存為 300 像素。 在這個例子中,我們將二維碼儲存為 600px 而不是 300px。

:path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-writing-12.cs
using IronQr;
using IronSoftware.Drawing;

QrStyleOptions styleOptions = new QrStyleOptions()
{
    // Change the dimensions to 600px
    Dimensions = 600,
};

string url = "https://ironsoftware.com/csharp/qr/";

// Create QR code
QrCode qr = QrWriter.Write(url);

// Save QR code as a bitmap
AnyBitmap qrImage = qr.Save(styleOptions);

// Save QR code bitmap as file
qrImage.SaveAs("qrURLResized.png");
Imports IronQr
Imports IronSoftware.Drawing

Private styleOptions As New QrStyleOptions() With {.Dimensions = 600}

Private url As String = "https://ironsoftware.com/csharp/qr/"

' Create QR code
Private qr As QrCode = QrWriter.Write(url)

' Save QR code as a bitmap
Private qrImage As AnyBitmap = qr.Save(styleOptions)

' Save QR code bitmap as file
qrImage.SaveAs("qrURLResized.png")
$vbLabelText   $csharpLabel

alt text

頁邊距和邊框

要調整邊距和邊框,我們可以使用QrStyleOptions類別的Margins屬性。 此屬性控制二維碼四周的邊距,預設值為 10px。 在我們的範例中,我們將邊距設定為 20px。

:path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-writing-13.cs
using IronQr;
using IronSoftware.Drawing;

QrStyleOptions styleOptions = new QrStyleOptions()
{
    // Change margins to 20px
    Margins = 20
};

string url = "https://ironsoftware.com/csharp/qr/";

// Create QR code
QrCode qr = QrWriter.Write(url);

// Save QR code as a bitmap
AnyBitmap qrImage = qr.Save(styleOptions);

// Save QR code bitmap as file
qrImage.SaveAs("qrURLMarginMultiple.png");
Imports IronQr
Imports IronSoftware.Drawing

Private styleOptions As New QrStyleOptions() With {.Margins = 20}

Private url As String = "https://ironsoftware.com/csharp/qr/"

' Create QR code
Private qr As QrCode = QrWriter.Write(url)

' Save QR code as a bitmap
Private qrImage As AnyBitmap = qr.Save(styleOptions)

' Save QR code bitmap as file
qrImage.SaveAs("qrURLMarginMultiple.png")
$vbLabelText   $csharpLabel

alt text

更改每一邊的邊距

IronQR 還允許使用者為每一邊指定不同的邊距,從而提供更精細的控制。

:path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-writing-14.cs
using IronQr;
using IronSoftware.Drawing;

QrStyleOptions styleOptions = new QrStyleOptions()
{
    // Change margins
    MarginBottom = 30,
    MarginTop = 100,
    MarginRight = 40,
    MarginLeft = 20,
};

string url = "https://ironsoftware.com/csharp/qr/";

// Create QR code
QrCode qr = QrWriter.Write(url);

// Save QR code as a bitmap
AnyBitmap qrImage = qr.Save(styleOptions);

// Save QR code bitmap as file
qrImage.SaveAs("qrURLMarginMultiple.png");
Imports IronQr
Imports IronSoftware.Drawing

Private styleOptions As New QrStyleOptions() With {
	.MarginBottom = 30,
	.MarginTop = 100,
	.MarginRight = 40,
	.MarginLeft = 20
}

Private url As String = "https://ironsoftware.com/csharp/qr/"

' Create QR code
Private qr As QrCode = QrWriter.Write(url)

' Save QR code as a bitmap
Private qrImage As AnyBitmap = qr.Save(styleOptions)

' Save QR code bitmap as file
qrImage.SaveAs("qrURLMarginMultiple.png")
$vbLabelText   $csharpLabel

重新著色

我們可以使用QrStyleOptions類別為二維碼及其背景添加顏色。 自訂顏色可以讓二維碼更加獨特、引人注目。 您可以使用ColorBackgroundColor屬性來變更顏色。 請務必匯入IronSoftware.Drawing包,以取得可指派顏色的清單。

:path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-writing-15.cs
using IronQr;
using IronSoftware.Drawing;

// Load new logo image
AnyBitmap logo = AnyBitmap.FromFile("sample.png");

// Add new logo to QR code style options
QrStyleOptions styleOptions = new QrStyleOptions()
{
    Logo = new QrLogo(logo, 50, 50, 10),
};

string url = "https://ironsoftware.com/csharp/qr/";

// Create QR code
QrCode qr = QrWriter.Write(url);

// Save QR code as a bitmap
AnyBitmap qrImage = qr.Save(styleOptions);

// Save QR code bitmap as file
qrImage.SaveAs("qrURLColored.png");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

alt text

添加徽標

除了顏色和尺寸,您還可以將公司徽標應用到二維碼上。 這有助於用戶立即識別並將二維碼與您的品牌聯繫起來。 透過Logo屬性,您可以輕鬆新增公司徽標來自訂二維碼。

:path=/static-assets/qr/content-code-examples/tutorials/csharp-qr-writing-15.cs
using IronQr;
using IronSoftware.Drawing;

// Load new logo image
AnyBitmap logo = AnyBitmap.FromFile("sample.png");

// Add new logo to QR code style options
QrStyleOptions styleOptions = new QrStyleOptions()
{
    Logo = new QrLogo(logo, 50, 50, 10),
};

string url = "https://ironsoftware.com/csharp/qr/";

// Create QR code
QrCode qr = QrWriter.Write(url);

// Save QR code as a bitmap
AnyBitmap qrImage = qr.Save(styleOptions);

// Save QR code bitmap as file
qrImage.SaveAs("qrURLColored.png");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

alt text

自訂徽標

QrLogo類別允許對徽標的外觀進行進一步自訂。 以下是可供選擇的房產:

  • Bitmap :表示您要用作標誌的圖像。
  • Width :表示標誌的寬度。 預設值為0。
  • Height :表示標誌的高度。 預設值為0。
  • CornerRadius :表示標誌邊角的圓角半徑。 預設值為 0,這表示徽標將具有直角。
using IronQRCode;
using IronSoftware.Drawing;

課程項目
{
    static void Main()
    {
        QrStyleOptions styleOptions = new QrStyleOptions
        {
            Logo = new QrLogo
            {
                Bitmap = AnyBitmap.FromBitmap("path/to/logo.png"),
                Width = 50,
                Height = 50,
                CornerRadius = 5
            }
        };

        QrCode qr = QrWriter.CreateQrCode()
                               .Write("Customized Logo Example");

        qr.SaveAs("example-customized-logo-qr.png", styleOptions);
    }
}
using IronQRCode;
using IronSoftware.Drawing;

課程項目
{
    static void Main()
    {
        QrStyleOptions styleOptions = new QrStyleOptions
        {
            Logo = new QrLogo
            {
                Bitmap = AnyBitmap.FromBitmap("path/to/logo.png"),
                Width = 50,
                Height = 50,
                CornerRadius = 5
            }
        };

        QrCode qr = QrWriter.CreateQrCode()
                               .Write("Customized Logo Example");

        qr.SaveAs("example-customized-logo-qr.png", styleOptions);
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

檢查容錯性

除了文件格式和自訂方面的廣泛靈活性外,這種靈活性還延伸到偵錯和錯誤處理方面。 IronQR 為開發人員提供各種工具來處理異常並編寫單元測試來驗證應用程式。

校驗和

二維碼有時可能會損壞,但 IronQR 內建了校驗及及資料糾錯功能,以保持其功能正常。 它採用里德-所羅門糾錯演算法,確保二維碼具有容錯能力。

詳細錯誤訊息

IronQR 提供詳細的錯誤訊息,幫助使用者快速識別問題。 這些訊息包含特定異常的列表,使偵錯和問題解決更加直接。 以下是該程式庫使用的IronQrException清單。

  • IronQrEncodingExceptionIronQrException的子類,當寫入二維碼時出現問題,會發生此錯誤。 例如,如果使用者嘗試從空字串建立二維碼,就會出現此提示。

alt text

  • IronQrFileExceptionIronQrException的子類,當出現與檔案相關的問題時,會發生此錯誤。

  • IronQrPdfPasswordExcceptionIronQrException的子類,當使用者嘗試加蓋印章的 PDF 受密碼保護,且未提供密碼或提供了錯誤的密碼時,會發生此錯誤。 它還涵蓋了其他與 PDF 相關的錯誤,例如 PDF 無法開啟的情況,如範例所示。

alt text

結論

IronQR 提供了一套全面的方法,用於在 .NET 應用程式中產生和自訂二維碼。 憑藉其強大的功能,開發人員可以輕鬆創建具有各種資料編碼、視覺樣式和糾錯等級的二維碼。 該庫支援多種輸出格式,並能無縫整合到現有文件中,使其成為任何二維碼專案的多功能工具。 無論您需要基本的二維碼還是高級的品牌解決方案,IronQR 都能提供靈活且功能強大的解決方案,高效滿足您的需求。

要了解更多信息,請查看IronQR 文檔,開始免費試用,並查看許可選項,看看哪個方案最適合您的需求。

常見問題解答

如何在 C# 中產生 QR 代碼?

您可以使用 IronQR 中提供的 QrWriter 類,在 C# 中產生 QR 碼。此類可讓您將資料寫入 QR 代碼,並將其儲存為各種圖像格式。只需使用 Write 方法來編碼您的資料,並使用 SaveAs 來輸出 QR 代碼。

QR 碼可以套用哪些類型的客製化?

IronQR 允許您自訂 QR 代碼,包括改變顏色、新增標誌、調整大小和邊界。使用 QrStyleOptions 類應用這些自訂。

我可以使用 C# 將 QR 代碼嵌入 PDF 嗎?

是的,您可以利用 StampToExistingPdfPageStampToExistingPdfPages 方法,使用 IronQR 將 QR 代碼嵌入 PDF。這允許您指定 QR 代碼應該出現的位置和頁面。

建立 QR 碼時,如何處理錯誤?

IronQR 具有強大的錯誤處理功能,提供 IronQrEncodingException, IronQrFileException, 和 IronQrPdfPasswordException 等錯誤訊息,以協助除錯及解決問題。

我可以將 QR 代碼匯出成哪些格式?

使用 IronQR,您可以將 QR 碼匯出成各種格式,包括 JPG、PNG、GIF 和 TIFF。SaveAs 方法可讓您指定所需的 QR 碼輸出格式。

此程式庫是否支援跨平台開發?

是的,IronQR 透過 Iron Software.Drawing 函式庫支援跨平台開發,使其相容於不同的 .NET 版本和平台。

是否可以在 QR 代碼中加入標誌以建立品牌?

您可以透過設定 QrStyleOptions 類別中的 Logo 屬性,使用 IronQR 在 QR 代碼中加入標誌,讓品牌 QR 代碼擁有客製化的標誌外觀。

QR 碼中錯誤修正的目的是什麼?

IronQR 支援 QR 碼的錯誤修正功能,可確保 QR 碼即使部分損壞仍可閱讀。此功能提供四種修正等級:最高、高、中、低,以符合不同的使用情況。

QR 碼可以編碼哪些資料類型?

IronQR 可以將多種資料類型編碼到 QR 碼中,包括文字、URL、數字、二進位資料和串流,讓您可以靈活地表示資料。

如何在 C# 中建立具有 URL 的 QR 碼?

若要在 C# 中建立具有 URL 的 QR 代碼,請使用 IronQR 中的 QrWriter 類。利用 Write 方法來編碼 URL,並利用 SaveAs 將 QR 代碼儲存為影像。

Curtis Chau
技術作家

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

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

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