跳過到頁腳內容
使用IRONBARCODE

如何為鏈接創建QR碼(C#教程)

1.0 引言

快速響應碼(QR碼)起源於日本汽車產業。 它也被稱為 QR 符號、QR 圖形或 QR 程式碼圖形。 原正弘於 1994 年發明了它,其最初的應用是作為一種快速簡便的工具,用於監控車輛在工廠組裝過程中的進度。 自那時起,二維碼作為條碼替代品的應用在汽車製造業以外的領域也得到了顯著增長。 造成這種情況的原因在於其更快的讀取速度和更大的儲存容量。 除了追蹤和識別產品外,二維碼還可以用於交換和儲存訊息,以用於行銷目的以及社交媒體網站和帳戶。 可以使用相機應用程式掃描應用程式產生的靜態二維碼。

2.0 IronBarcode 功能

借助 IronBarcode 的二維碼庫,建立動態二維碼變得非常簡單。 只要幾行程式碼,這個簡單易用的函式庫就能快速產生二維碼。 IronBarcode 可以產生高品質的二維碼,並且使二維碼掃描器能夠輕鬆讀取自訂程式碼。

  • IronBarcode 可以讀取和寫入大多數條碼格式和二維碼標準,包括 UPC A/E、EAN 8/13、Code 39/93/128、ITF、MSI、RSS 14/Expanded、Databar 和 CodaB。
  • IronBarcode 可以讀取掃描影像和即時視訊幀,校正旋轉、雜訊、失真和傾斜。 IronBarcode 在建立二維碼時會自動預處理條碼影像,以提高讀取效率和準確性。 它們允許內容編輯,動態二維碼尤其受歡迎。
  • IronBarcode 可以在多個核心和執行緒上運行(這對批次伺服器尤其有用)。
  • IronBarcode 可以自動定位單頁和多頁文件中的一個或多個條碼。
  • IronBarcode 可與 .NET Framework 和 .NET Core 實作搭配使用,並支援 32 位元和 64 位元架構。
  • IronBarcode 在 PC 和行動平台上支援控制台、桌面、雲端和 Web 應用程式。
  • PDF、JPG、TIFF、GIF、BMP、PNG 和 HTML 只是 IronBarcode 可以產生二維碼圖片的幾種檔案和串流格式。

3.0 建立二維碼影像

3.1 從 Windows/控制台應用程式使用 IronBarcode

本文的後續部分提供了一個示例,以演示我們的庫如何輕鬆生成QR碼。

第一步:建立一個用於產生二維碼的新項目

開啟 Visual Studio,然後按一下"檔案"功能表選項中的"新專案"

在隨後出現的對話方塊中選擇控制台應用程式範本後,按一下"下一步"。

如何為連結建立二維碼(C# 教學):圖 1

在"項目名稱"文字區域中輸入您選擇的任何項目名稱(例如,二維碼產生器),然後在"位置"欄位中輸入新項目的位置。 點擊"下一步"按鈕繼續。

如何為連結建立二維碼(C# 教學):圖 2

從框架下拉選項中選擇一個 .NET Framework(這裡我們使用的是 .NET 6.0(長期支援)),然後按一下"建立"。

如何為連結建立二維碼(C# 教學):圖 3

步驟 2. 安裝 IronBarcode 庫

透過在軟體包管理器控制台中輸入以下程式碼來下載所需的 IronBarcode 庫:

Install-Package BarCode

如何為連結建立二維碼(C# 教學):圖 4

或者,我們可以使用 NuGet 套件管理進行搜尋並下載"IronBarcode"套件,這將列出所有結果,並允許我們選擇需要下載的套件。

如何為連結建立二維碼(C# 教學):圖 5

步驟 3:產生二維碼影像

IronBarcode 讓我們可以用幾行程式碼建立二維碼。 使用QRCodeWriter.CreateQrCode方法建立一個新的二維碼,如下面的程式碼所示:

using IronBarCode;

// Create a QR code with a specified URL and dimensions,
// and save it as a PNG file.
QRCodeWriter.CreateQrCode("www.google.com", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0).SaveAsPng("NewQR.png");
using IronBarCode;

// Create a QR code with a specified URL and dimensions,
// and save it as a PNG file.
QRCodeWriter.CreateQrCode("www.google.com", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0).SaveAsPng("NewQR.png");
Imports IronBarCode

' Create a QR code with a specified URL and dimensions,
' and save it as a PNG file.
QRCodeWriter.CreateQrCode("www.google.com", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0).SaveAsPng("NewQR.png")
$vbLabelText   $csharpLabel

要編碼到二維碼影像中的資料(可以是 URL 或 Stream)是CreateQrCode方法的唯一必要參數。 此外,該方法還接受三個可選參數:

  • 圖形的預設尺寸為寬 500 像素,高 500 像素。
  • 糾錯級別。 IronBarcode 提供低、中、高、最高四個等級的糾錯能力。 CreateQrCode方法預設使用最高等級的糾錯( QRCodeWriter.QrErrorCorrectionLevel.Highest )。
  • 二維碼的版本號。 如需查看可接受的變體列表,請造訪頁面。 如果值為 0(預設值),則方法會根據要編碼的資料使用正確的版本號。

上面的範例使用中等程度的糾錯功能產生 500 x 500 像素的圖形,從而建立自訂二維碼。 接下來,我們可以透過呼叫SaveAsPng方法,將產生的二維碼儲存為 PNG 檔案到指定的檔案位置。

如何為連結建立二維碼(C# 教學):圖 6

接下來,我們將看一個用例範例,其中使用者或企業希望將公司徽標添加到生成的二維碼中。 為了實現這一點,下面的範例程式碼中使用了QrCodeWriter.CreateQrCodeWithLogo方法。

using System.Drawing; // Necessary for Color type

// Create a QR code with a logo included.
var QRWithLogo = QRCodeWriter.CreateQrCodeWithLogo("www.google.com", "qrWithlogo.png", 500);

// Customize the color of the QR code using the ChangeBarCodeColor method.
QRWithLogo.ChangeBarCodeColor(Color.DarkRed);

// Save the customized QR code as a new PNG file.
QRWithLogo.SaveAsPng("NewQR_Code.png");
using System.Drawing; // Necessary for Color type

// Create a QR code with a logo included.
var QRWithLogo = QRCodeWriter.CreateQrCodeWithLogo("www.google.com", "qrWithlogo.png", 500);

// Customize the color of the QR code using the ChangeBarCodeColor method.
QRWithLogo.ChangeBarCodeColor(Color.DarkRed);

// Save the customized QR code as a new PNG file.
QRWithLogo.SaveAsPng("NewQR_Code.png");
Imports System.Drawing ' Necessary for Color type

' Create a QR code with a logo included.
Private QRWithLogo = QRCodeWriter.CreateQrCodeWithLogo("www.google.com", "qrWithlogo.png", 500)

' Customize the color of the QR code using the ChangeBarCodeColor method.
QRWithLogo.ChangeBarCodeColor(Color.DarkRed)

' Save the customized QR code as a new PNG file.
QRWithLogo.SaveAsPng("NewQR_Code.png")
$vbLabelText   $csharpLabel

在上面的範例中,我們將 URL"www.google.com"的字串值編碼成一個新的二維碼,該二維碼將該網站上的圖像嵌入到指定的檔案位置。

上面的二維碼中包含圖片。 徽標會自動定位到該尺寸,並調整大小以匹配二維碼的方形網格,以便仍可讀取純程式碼。 我們也可以使用ChangeBarCodeColor方法自訂 QR 位址條碼的顏色,該方法提供了一系列可用於 QR 碼的程式碼顏色。 一種方法使用顏色類別類型,另一種方法則使用 HTML 十六進位顏色表示法,如下所示:

QRWithLogo.ChangeBarCodeColor(System.Drawing.ColorTranslator.FromHtml("#8B0000"));
QRWithLogo.ChangeBarCodeColor(System.Drawing.ColorTranslator.FromHtml("#8B0000"));
QRWithLogo.ChangeBarCodeColor(System.Drawing.ColorTranslator.FromHtml("#8B0000"))
$vbLabelText   $csharpLabel

上述程式碼行指定了條碼的深紅色。 然後,按照代碼指示,我們將其儲存為 PNG 檔案。我們也可以將二維碼儲存為其他檔案格式,例如 HTML:

QRWithLogo.SaveAsHtmlFile("test.html");
QRWithLogo.SaveAsHtmlFile("test.html");
QRWithLogo.SaveAsHtmlFile("test.html")
$vbLabelText   $csharpLabel

上述原始碼的運行結果如下圖所示。

如何為連結建立二維碼(C# 教學):圖 7

二維碼可以儲存為其他檔案格式,例如 HTML:

QRWithLogo.SaveAsHtmlFile("test.html");
QRWithLogo.SaveAsHtmlFile("test.html");
QRWithLogo.SaveAsHtmlFile("test.html")
$vbLabelText   $csharpLabel

如何為連結建立二維碼(C# 教學):圖 8

使用 IronBarcode 二維碼產生器網站

IronBarcode 也可以用於 Web 應用程式。 下面提供了 MVC Dot Net core 6.0 範例程式碼。

public IActionResult Index()
{
    // Generate a QR code for www.google.com
    var barcode = QRCodeWriter.CreateQrCode("www.google.com", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0);

    // Save the QR code as a PNG file
    barcode.SaveAsPng("Barcode.png");

    // Get the file path to the saved QR code
    var filePath = Path.Combine(Directory.GetCurrentDirectory(), "Barcode.png");

    // Return the file as a physical file result to be downloaded
    return PhysicalFile(filePath, "image/png", "Barcode.png");
}
public IActionResult Index()
{
    // Generate a QR code for www.google.com
    var barcode = QRCodeWriter.CreateQrCode("www.google.com", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0);

    // Save the QR code as a PNG file
    barcode.SaveAsPng("Barcode.png");

    // Get the file path to the saved QR code
    var filePath = Path.Combine(Directory.GetCurrentDirectory(), "Barcode.png");

    // Return the file as a physical file result to be downloaded
    return PhysicalFile(filePath, "image/png", "Barcode.png");
}
Public Function Index() As IActionResult
	' Generate a QR code for www.google.com
	Dim barcode = QRCodeWriter.CreateQrCode("www.google.com", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0)

	' Save the QR code as a PNG file
	barcode.SaveAsPng("Barcode.png")

	' Get the file path to the saved QR code
	Dim filePath = Path.Combine(Directory.GetCurrentDirectory(), "Barcode.png")

	' Return the file as a physical file result to be downloaded
	Return PhysicalFile(filePath, "image/png", "Barcode.png")
End Function
$vbLabelText   $csharpLabel

我們用於 Windows/控制台程式的程式碼與上面提供的程式碼相同。 上述程式碼首先產生二維碼文件,然後再返回該文件。 然後用戶可以將二維碼下載為PNG檔案。我們還可以使其可在行動裝置和線上表單上使用。

如何為連結建立二維碼(C# 教學):圖 9

如何為連結建立二維碼(C# 教學):圖 10

想了解更多關於IronBarcode的程式碼教學,請點擊這裡

4.0 結論

IronBarcode 是建立和識別條碼最有效的程式庫之一。 此外,它也是創建和讀取條碼速度最快的庫之一。 該庫與不同的作業系統相容。

它開發簡單,並支援多種條碼格式。 我們可以更改顏色、線條粗細、高度、條碼文字等等。

您可以在這裡找到許可資訊。 IronBarcode 提供免費的開發者授權版二維碼產生器庫和進階版。 包含一年的免費支援和更新。

用戶還可以從Iron Suite中受益,Iron Suite 是一套包含 5 個專業 ASP.NET core 庫的套件,其中包括IronBarcodeIronXLIronPDF等。

常見問題解答

QR 碼在現代應用程式中的意義是什麼?

QR 代碼提供了一種快速有效的方式來儲存和交換資訊,使其在許多現代應用程式中變得不可或缺,例如行銷、產品追蹤和付款系統。IronBarcode 可以輕鬆地為這些應用程式產生和自訂 QR 代碼。

如何用 C# 建立網頁連結的 QR 碼?

您可以使用 IronBarcode,以 URL 為參數,呼叫 QRCodeWriter.CreateQrCode 方法來建立網頁連結的 QR 碼。自訂 QR 代碼的外觀,並將其儲存為 PNG 或 PDF 等格式。

使用 IronBarcode 生成 QR 代碼有哪些優勢?

IronBarcode 提供高品質的 QR 代碼產生功能,並具備自訂功能,例如顏色變更和標誌嵌入。它支援多種條碼格式和檔案輸出,使其適用於不同的應用程式。

如何將 QR 碼產生整合到 .NET 網路應用程式中?

若要在 .NET 網路應用程式中整合 QR 碼產生功能,請使用 IronBarcode 與 MVC Dot Net core 6.0。該函式庫可讓您在 Web 應用程式中動態產生 QR 碼,並支援各種下載格式。

我可以使用函式庫自訂 QR 碼尺寸和錯誤修正等級嗎?

是的,IronBarcode 允許您自訂 QR 代碼的尺寸和錯誤修正等級。使用CreateQrCode方法與特定參數,可根據您的需求調整這些功能。

如何使用 C# 在 QR 代碼中加入標誌?

要在 C# 中為 QR 代碼添加標誌,請使用 IronBarcode 的 QRCodeWriter.CreateQrCodeWithLogo 方法。此功能可讓您在 QR 代碼中嵌入標誌,在保持代碼可讀性的同時提高品牌知名度。

QR 代碼產生問題有哪些疑難排解技巧?

如果您遇到 QR 代碼產生的問題,請確保透過 NuGet 正確安裝函式庫,並按照說明使用所有方法。檢查您的專案設定或 .NET 版本是否有任何相容性問題。

使用函式庫產生的 QR 碼,支援哪些檔案格式儲存?

IronBarcode 支援以多種檔案格式儲存 QR 代碼,包括 PDF、JPG、TIFF、GIF、BMP、PNG 和 HTML,為不同的使用情況提供彈性。

QR 代碼產生函式庫是否有免費版本?

是的,IronBarcode 為開發人員提供免費版本的 QR 代碼產生函式庫。此外,還有包含額外功能、支援和更新的高級版本。

Jordi Bardia
軟體工程師
Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担产品测测试,产品开发和研究的责任时,Jordi 为持续的产品改进增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。