
How to Make A QR Code For A Link (C# Tutorial)
1.0 介绍
快捷响应码,或称为QR码,其根源在于日本的汽车行业。 它也被称为QR符号、QR图形或QR代码图形。 它是由原昌宏于1994年创造的,最初的应用是在汽车装配过程中,作为一种快速简单的工具来监控车辆的进度。 自那时起,QR码作为条形码替代品的使用在汽车制造业之外大幅增长。 这都归功于其较快的读取速度和存储容量。 QR码可用于交换和存储信息,用于营销目的以及社交媒体网站和帐户,除了跟踪和识别产品之外。 可以用相机应用程序扫描从应用程序生成的静态QR码。
2.0 IronBarcode 特性
使用IronBarcode的QR码库,创建动态QR码变得简单。 只需几行代码,这个简单的库就能快速创建QR码。 IronBarcode可以生成高质量的QR码,并且使QR码扫描器能轻松读取自定义代码。
- 包括UPC A/E、EAN 8/13、Code 39/93/128、ITF、MSI、RSS 14/扩展、Databar和CodaB在内的大多数条形码格式和QR标准都可以被IronBarcode读取和编写。
- IronBarcode可以读取扫描和实时视频帧,纠正旋转、噪声、变形和倾斜。 IronBarcode自动预处理条码图像,以提高创建QR码时的读取效率和准确性。 它们允许内容编辑,动态二维码尤其受欢迎。
- IronBarcode可以在多个核心和线程上运行(特别适用于批处理服务器)。
- IronBarcode能够在单页和多页文档中自动定位一个或多个条形码。
- IronBarcode适用于.NET Framework和.NET Core实现,并支持32位和64位架构。
- 在PC和移动平台上,IronBarcode支持控制台、桌面、云和网络应用程序。
- IronBarcode可以为PDF、JPG、TIFF、GIF、BMP、PNG和HTML等文件和流格式生成QR码图片。
3.0 创建QR码图像
3.1 在Windows/控制台应用程序中使用IronBarcode
The proceeding sections of this article present an example to demonstrate how easily our library generates QR codes.
步骤1. 创建新项目以创建QR码
打开Visual Studio,然后点击文件菜单中的新建项目选项。
在后续窗口中选择控制台应用模板后点击下一步。

在项目名称文本区输入任意项目名称(例如,QR码生成器),然后在位置字段中输入新项目的位置。 然后点击下一步按钮继续。

从Framework下拉选项中选择一个.NET Framework(此处,我们使用.NET 6.0(长期支持)),然后点击创建。

步骤2. 安装IronBarcode库
通过在包管理器控制台中输入以下代码来下载必要的IronBarcode库:

或者,我们可以使用NuGet包管理进行搜索并下载"IronBarcode"包,这将列出所有结果,并允许我们选择需要下载的包。

步骤3. 生成QR码图像
IronBarcode允许我们用几行代码创建QR码。 使用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");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")要编码到代码图像中的数据(可能是URL或流)是CreateQrCode方法的唯一必需参数。 此外,该方法还接受三个可选参数:
- 图形的默认尺寸为500px宽乘以500px高。
- 错误更正级别。 IronBarcode提供低、中、高和最高四种错误更正级别。
QRCodeWriter.QrErrorCorrectionLevel.Highest)。 - QR码的版本号。 有关可接受变体的列表,请访问此页面。 如果值为0(默认值),则根据将编码的数据来给方法指定正确的版本号。
上面的示例使用中等错误更正级别来生成500乘500px的图形,从而创建一个自定义QR码。 接下来,我们可以调用SaveAsPng方法,将生成的二维码保存为指定文件位置的PNG文件。

接下来,我们来看一个用例示例,即用户或企业想要在生成的QR码中加入公司标识。 在下面的示例代码中,使用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");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")在上述示例中,我们将网址"www.google.com"的字符串值编码到一个新的QR码中,该码将该网站的图片嵌入到给定的文件位置。
QR码中包括了图形。 标识被自动调整到对应的大小,以匹配QR码的正方形网格,使纯代码仍然可读。 我们还可以使用ChangeBarCodeColor方法自定义QR地址条形码的颜色,提供了一系列可在QR码上使用的代码颜色。 一种使用Color类类型,另一种使用HTML十六进制颜色表示法,如下所示:
QRWithLogo.ChangeBarCodeColor(System.Drawing.ColorTranslator.FromHtml("#8B0000"));QRWithLogo.ChangeBarCodeColor(System.Drawing.ColorTranslator.FromHtml("#8B0000"))以上代码行指定条码的深红色。 然后,根据代码指示,我们将其保存为PNG文件。我们还可以以其他文件格式保存QR码,例如HTML:
QRWithLogo.SaveAsHtmlFile("test.html");QRWithLogo.SaveAsHtmlFile("test.html")上述源代码的结果显示在下面的图片中。

QR码可以以其他文件格式保存,如HTML:
QRWithLogo.SaveAsHtmlFile("test.html");QRWithLogo.SaveAsHtmlFile("test.html")
使用IronBarcode QR码生成器网站
IronBarcode也可用于网络应用。 下面提供了MVC .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 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我们用于Windows/控制台程序的代码与上面提供的代码相同。 上述代码首先生成一个QR码文件,然后返回它。 用户可以将二维码以PNG文件的形式下载。我们还可以使其可用于移动设备和在线表单。


点击这里获取更多IronBarcode代码教程。
4.0 结论
IronBarcode是用于创建和识别条形码的最有效的库之一。 此外,它也是创建和读取条形码最快的库之一。 该库可以与不同的操作系统兼容。
它易于开发且支持多种条码格式。 我们可以更改颜色、线宽、高度、条码文本等。
您可以在这里找到许可信息。 IronBarcode提供了一个免费的开发者许可版QR码生成器库,以及一个优质版本。 它包括一年的免费支持和更新。
用户还可以得益于[Iron Suite](Iron Suite),它是一套包括IronBarcode,IronXL,IronPDF在内的五个专业ASP.NET Core库。

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。
相关文章


