# Write QR Codes in C#
## 简介
通过 IronQR,开发人员可以为流行的图片格式创建二维码,并用背景颜色、边距、徽标对其进行自定义,甚至将其添加到 PDF 中。 对于高级用途,它还提供纠错和版本控制功能。
本文将结合实例探讨 IronQR 的主要功能,帮助您了解如何使用它在 C# 中编写二维码,并将其有效地应用到您的项目中。
## 目录
- **输入数据**
- [文本、URLs、数字](#anchor-text-urls-numbers)
- [ 二进制和流](#anchor-binary-streams)
- **导出 QR 代码**
- [ 另存为图像](#anchor-save-as-image)
- [System.Drawing.Images](#anchor-system-drawing-images)
- [IronSoftware.Drawing](#anchor-ironsoftware-drawing)
- [Stamp on PDF](#anchor-stamp-on-pdf)
- **QR 代码选项**
- [编码](#anchor-encoding)
- [ 错误纠正](#anchor-error-correction)
- [QR 代码版本](#anchor-qr-code-version)
- [字符编码](#anchor-character-encoding)
- **QR 代码样式**
- [ 调整大小](#anchor-resize)
- [边距和边框](#anchor-margins-borders)
- [Recolor](#anchor-recolor)
- [ 添加徽标](#anchor-add-a-logo)
!!!--LIBRARY_START_TRIAL_BLOCK--!!!
## 输入数据
### 文本、URL、数字
IronQR 可以将文本、URL 和数字等多种数据类型转换成 QR 码。 无论您是为市场营销和交流创建二维码链接或文本,还是为库存管理创建数字代码,抑或是将二进制数据或数据流编码为可读的二维码,IronQR 都能为您提供所需的一切支持。
此外,API 要简单明了。 `QrWriter` 类提供了多种重载,允许不同类型的数据作为输入,从而简化复杂性并优化流程。
```csharp
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");
```
### 二进制和流
同样地,我们可以使用前面提到的相同 `Write` 方法将二进制数据和流转换为QR码。
```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");
```
`Write` 方法有重载,接受字节数组和流作为输入。 对于流,我们可以从字节数组创建 `MemoryStream`,然后将其转换为QR码。 当用户需要对数据块进行更精细的控制时,这将非常有用,因为流可以更节省内存。
```cs
using IronQr;
using IronSoftware.Drawing;
using System.IO;
using System.Text;
MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/qr/"));
// Create QR code
QrCode streamQr = QrWriter.Write(stream);
// Save QR code as a bitmap
AnyBitmap streamQrImage = streamQr.Save();
// Save QR code bitmap as file
streamQrImage.SaveAs("streamQr.png");
```
---
## 导出 QR 代码
IronQR 具有灵活性,可适应需要不同文件格式的各种用例。 您可以使用 `SaveAs` 方法将QR码保存为多种格式,例如 JPG、PNG、GIF 和 TIFF。
### 保存为图像
来自 `AnyBitmap` 的 `SaveAs` 方法会根据提供的文件路径自动检测文件格式。 在这个例子中,我指定了一个以 `.png` 结尾的文件路径。
[[i:(使用 `SaveAs` 方法时,请注意没有默认的图像格式。 (如果您输入了无法识别的扩展名或在文件路径中输入错误,将以不正确的扩展名保存图像。)]]
```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");
```
### System.Drawing.Images
将图像转换为来自Microsoft的 `System.Drawing.Images` 对象,使您可以使用 `Bitmap` 类将QR码保存到文件路径。 在这个例子中,`Save` 方法将QR码保存为PNG格式的文件到路径 `qrBitmap.png`。
`System.Drawing.Common` 仅支持在Windows平台上使用。
```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");
```
### IronSoftware.Drawing
由于 `System.Drawing.Common` 缺乏跨平台兼容性,开发者可能在维护跨平台应用程序时遇到问题。 IronQR 可以利用 `System.Drawing.Common` 和 `IronSoftware.Drawing`。
IronQR 使用来自 [`IronSoftware.Drawing`](https://ironsoftware.com/open-source/csharp/drawing/docs/) 的 `AnyBitmap` 类,这是一个具有通用兼容性的位图类,可以隐式转换为以下内容:
- `System.Drawing.Bitmap`
- `System.Drawing.Image`
- `SkiaSharp.SKBitmap`
- `SixLabors.ImageSharp`
- `Microsoft.Maui.Graphics.Platform.PlatformImage`
借助这个强大的开源库,IronQR 实现了跨平台支持,并与 `.NET 10`、`.NET 9`、`.NET 8`、`.NET 7`、`.NET 6`、`.NET Core`、`.NET Standard` 和 `.NET Framework 4.6.2+` 兼容。 要了解有关该库的更多信息,请访问[IronSoftware.Drawing](https://ironsoftware.com/open-source/csharp/drawing/)网站。
### 在 PDF 上盖章
IronQR 允许开发人员在现有 PDF 文档上加盖 QR 码,方便他人快速访问链接或其他资源。 支持在单页和多页上印制二维码。
#### 加盖单页印章
创建QR码后,调用 `QrCode` 对象的 `StampToExistingPdfPage` 方法。 此方法需要提供文件路径、坐标(x 和 y)、页码以及密码(如果 PDF 受密码保护)。 提供参数后,该方法会在 QR 代码上盖章并保存 PDF。
[[i:(此方法基于PDF页面,页码从1而非0开始。)]]
```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);
```
#### 标记为多个页面
与上述示例类似,主要区别在于 `StampToExistingPdfPages` 方法接受的是页码列表而不是单个页码。
```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);
```
#### 两个示例的输出

---
## QR 代码选项
IronQR 提供广泛的自定义选项,可对 QR 代码行为和功能进行微调。 `QrOptions` 类提供了多个参数,如版本控制、编码类型、字符编码和错误纠正级别。 让我们来详细探讨一下这些选项。
### 编码
IronQR 支持创建和读取多种类型的二维码。 以下是支持的类型:
- `QRCode`:这是标准的QR码,今天常用。 它最多可存储 7089 个数字字符或 4296 个字母数字字符。
- `MicroQRCode`:标准QR码的小型版本,可以存储最多35个数字字符或21个字母数字字符。
- `RMQRCode`:矩形微卷QR码是QR码的紧凑版本,提供了宽高比的灵活性。
```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");
```
### 错误修正
IronQR 采用标准 QR 纠错技术,确保生成的所有 QR 代码即使在恶劣条件下也具有容错性和可靠性。 此外,IronQR 允许您完全控制纠错级别,以便进一步微调。
提供 `QrErrorCorrectionLevel` 的有四个错误纠正级别:
- `Highest`:30%错误纠正
- `High`:25%错误纠正
- `Medium`:15%错误纠正
- `Low`:7%错误纠正
```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");
```
与低纠错率的二维码相比,高纠错率的二维码在读取时具有更强的容错能力,更有可能在较低分辨率下被扫描。 您可以根据自己的使用情况进行测试。

### QR 代码版本
您可以调整二维码版本,以存储更多数据。 高版本是库存或物流的理想选择,而低版本则适用于较小的数据,如短 URL。 只需更改 `QrOptions` 对象中的 `Version` 属性,并将其传递给 `Write` 方法即可根据需要生成QR码。
```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");
```

从输出结果中可以看出,与第 5 版相比,第 40 版的二维码非常复杂和密集。
低版本需要更精确的扫描,没有更高分辨率的扫描仪可能难以扫描。 不过,更高的版本更容易扫描,即使使用较低分辨率的相机也是如此。 有关根据容量选择 QR 版本的更详细指南,请参阅 [QR 版本列表](https://www.qrcode.com/en/about/version.html)。
### 字符编码
该选项决定了 QR 代码的编码方式。 在我们的示例中,我们将其改为 "UTF-32",而默认字符编码为 "ISO-8859-1"。
```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");
```
---
## QR 代码样式
除了简单易用的方法和处理输入数据的灵活性,IronQR 还提供了许多自定义和风格化 QR 代码的选项,使其独一无二。 `QrStyleOptions` 类提供了用于自定义QR码各个方面的各种参数。 让我们来探讨一下可用的选项。
### 调整大小
要调整QR码的大小,您可以设置 `QrStyleOptions` 对象的 `Dimensions` 属性,然后将其传递给 `Save` 方法。 默认情况下,二维码的尺寸为 300px。 在本示例中,我们将二维码的尺寸保存为 600px,而不是 300px。
```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");
```

### 边距和边框
要调整页边距和边框,我们可以使用 `QrStyleOptions` 类的 `Margins` 属性。 该属性可控制二维码各边的边距,默认值为 10px。 在我们的示例中,我们将页边距设置为 20px。
```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");
```

#### 更改每页边距
IronQR 还允许用户为每一面指定不同的页边距,提供更精细的控制。
```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");
```
### 颜色
我们可以使用 `QrStyleOptions` 类为QR码及其背景添加颜色。 自定义颜色可以使 QR 代码更加独特和醒目。 您可以使用 `Color` 和 `BackgroundColor` 属性更改颜色。 请确保导入 `IronSoftware.Drawing` 以获取可分配色的列表。
```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");
```

### 添加徽标
除了颜色和尺寸,您还可以将公司徽标应用到 QR 代码中。 这有助于用户立即识别二维码并将其与您的品牌联系起来。 `Logo` 属性使您可以轻松地通过添加您的企业Logo来自定义QR码。
```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");
```

#### 自定义徽标
`QrLogo` 类允许进一步自定义Logo的外观。 以下是可用属性:
- `Bitmap`:表示您要用作Logo的图像。
- `Width`:表示Logo的宽度。 默认值为 0。
- `Height`:表示Logo的高度。 默认值为 0。
- `CornerRadius`:表示圆角Logo的半径。 默认情况下,它被设置为 0,这意味着徽标的边角将是方形的。
```csharp
using IronQr;
using IronSoftware.Drawing;
class Program
{
static void Main()
{
QrStyleOptions styleOptions = new QrStyleOptions
{
Logo = new QrLogo
{
Bitmap = AnyBitmap.FromFile("path/to/logo.png"),
Width = 50,
Height = 50,
CornerRadius = 5
}
};
QrCode qr = QrWriter.Write("Customized Logo Example");
AnyBitmap img = qr.Save(styleOptions);
img.SaveAs("example-customized-logo-qr.png");
}
}
```
---
## 检查容错能力
除了文件格式和自定义方面的广泛灵活性,灵活性还延伸到调试和错误处理方面。 IronQR 为开发人员提供了处理异常和编写单元测试以验证应用程序的各种工具。
### 检查和
QR 代码有时可能会损坏,但 IronQR 内置了校验和和数据校正功能,可以保证 QR 代码正常运行。 它使用 [Reed-Solomon 纠错算法](https://en.wikipedia.org/wiki/Reed%E2%80%93Solomon_error_correction),确保 QR 代码保持容错性。
### 详细的错误信息
IronQR 提供详细的错误信息,帮助用户快速发现问题。 这些信息包含具体的异常列表,使调试和解决问题更加简单明了。 以下是库中使用的 `IronQrException` 列表。
- `IronQrEncodingException`:`IronQrException` 的子类,当写入QR码时出现问题时发生此错误。 例如,如果用户试图用空字符串创建二维码,就会出现这样的提示。

- `IronQrFileException`:`IronQrException` 的子类,当出现文件相关问题时发生此错误。
- `IronQrPdfPasswordException`:`IronQrException` 的子类,当用户试图添加水印的PDF受密码保护且未提供密码或提供错误密码时发生此错误。 它还涵盖了其他与 PDF 相关的错误,例如当 PDF 无法打开时,如示例所示。

## 结论
IronQR 为在 .NET 应用程序中生成和定制 QR 代码提供了一套全面的方法。 凭借其强大的功能,开发人员可以轻松创建具有各种数据编码、视觉样式和纠错级别的 QR 代码。 该库支持多种输出格式,并可无缝集成到现有文档中,是任何二维码项目的通用工具。 无论您需要的是基本的 QR 码还是高级的品牌解决方案,IronQR 都能提供灵活性和功能性,高效满足您的需求。
要了解更多信息,请查看[IronQR 文档](https://ironsoftware.com/csharp/qr/docs/),从[免费试用](trial-license)开始探索,并查看[许可选项](https://ironsoftware.com/csharp/qr/licensing/),了解哪种计划最适合您的需求。
using IronQr;using IronSoftware.Drawing;string text = "Hello, World!";string url = "https://ironsoftware.com/csharp/qr/";string alphanumeric = "WATERSKU-12356";// Create QR codeQrCode textQr = QrWriter.Write(text);// Save QR code as a bitmapAnyBitmap textQrImage = textQr.Save();// Save QR code as filetextQrImage.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");
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");
ImportsIronQrImportsIronSoftware.DrawingPrivate text AsString = "Hello, World!"Private url AsString = "https://ironsoftware.com/csharp/qr/"Private alphanumeric AsString = "WATERSKU-12356"' Create QR codePrivate textQr AsQrCode = QrWriter.Write(text)' Save QR code as a bitmapPrivate textQrImage AsAnyBitmap = textQr.Save()' Save QR code as filetextQrImage.SaveAs("textQr.png")Dim urlQr AsQrCode = QrWriter.Write(url)Dim urlQrImage AsAnyBitmap = urlQr.Save()urlQrImage.SaveAs("urlQr.png")Dim alphanumericQr AsQrCode = QrWriter.Write(alphanumeric)Dim alphanumericQrImage AsAnyBitmap = 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")
二进制和流
同样地,我们可以使用前面提到的相同 Write 方法将二进制数据和流转换为QR码。
using IronQr;using IronSoftware.Drawing;using System.Text;byte[] bytes = Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/qr/");// Create QR codeQrCode bytesQr = QrWriter.Write(bytes);// Save QR code as a bitmapAnyBitmap qrImage = bytesQr.Save();// Save QR code bitmap to fileqrImage.SaveAs("bytesQr.png");
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");
ImportsIronQrImportsIronSoftware.DrawingImportsSystem.TextPrivate bytes() AsByte = Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/qr/")' Create QR codePrivate bytesQr AsQrCode = QrWriter.Write(bytes)' Save QR code as a bitmapPrivate qrImage AsAnyBitmap = bytesQr.Save()' Save QR code bitmap to fileqrImage.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")
using IronQr;using IronSoftware.Drawing;using System.IO;using System.Text;MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/qr/"));// Create QR codeQrCode streamQr = QrWriter.Write(stream);// Save QR code as a bitmapAnyBitmap streamQrImage = streamQr.Save();// Save QR code bitmap as filestreamQrImage.SaveAs("streamQr.png");
using IronQr;
using IronSoftware.Drawing;
using System.IO;
using System.Text;
MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/qr/"));
// Create QR code
QrCode streamQr = QrWriter.Write(stream);
// Save QR code as a bitmap
AnyBitmap streamQrImage = streamQr.Save();
// Save QR code bitmap as file
streamQrImage.SaveAs("streamQr.png");
ImportsIronQrImportsIronSoftware.DrawingImportsSystem.IOImportsSystem.TextPrivate stream As New MemoryStream(Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/qr/"))' Create QR codePrivate streamQr AsQrCode = QrWriter.Write(stream)' Save QR code as a bitmapPrivate streamQrImage AsAnyBitmap = streamQr.Save()' Save QR code bitmap as filestreamQrImage.SaveAs("streamQr.png")
Imports IronQr
Imports IronSoftware.Drawing
Imports System.IO
Imports System.Text
Private stream As New MemoryStream(Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/qr/"))
' Create QR code
Private streamQr As QrCode = QrWriter.Write(stream)
' Save QR code as a bitmap
Private streamQrImage As AnyBitmap = streamQr.Save()
' Save QR code bitmap as file
streamQrImage.SaveAs("streamQr.png")
using IronQr;using IronSoftware.Drawing;// Create a QR code objectQrCode qr = QrWriter.Write("hello world");// Save QR code as a bitmapAnyBitmap qrImage = qr.Save();// Save QR code bitmap as fileqrImage.SaveAs("qr.png");
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");
ImportsIronQrImportsIronSoftware.Drawing' Create a QR code objectPrivate qr AsQrCode = QrWriter.Write("hello world")' Save QR code as a bitmapPrivate qrImage AsAnyBitmap = qr.Save()' Save QR code bitmap as fileqrImage.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")
using IronQr;using System.Drawing;// Create a QR code objectQrCode qr = QrWriter.Write("hello world");// Save QR code as a bitmapBitmap qrImage = qr.Save();// Save QR code bitmap as fileqrImage.Save("qrBitmap.png");
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");
ImportsIronQrImportsSystem.Drawing' Create a QR code objectPrivate qr AsQrCode = QrWriter.Write("hello world")' Save QR code as a bitmapPrivate qrImage AsBitmap = qr.Save()' Save QR code bitmap as fileqrImage.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")
IronQR 允许开发人员在现有 PDF 文档上加盖 QR 码,方便他人快速访问链接或其他资源。 支持在单页和多页上印制二维码。
加盖单页印章
创建QR码后,调用 QrCode 对象的 StampToExistingPdfPage 方法。 此方法需要提供文件路径、坐标(x 和 y)、页码以及密码(如果 PDF 受密码保护)。 提供参数后,该方法会在 QR 代码上盖章并保存 PDF。
请注意: 此方法基于PDF页面,页码从1而非0开始。
using IronQr;// Create a QR code objectQrCode 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 1qr.StampToExistingPdfPage(filepath, x, y, page);
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);
ImportsIronQr' Create a QR code objectPrivate qr AsQrCode = QrWriter.Write("hello world")Private filepath AsString = "example.pdf"Private x AsInteger = 100Private y AsInteger = 150Private page AsInteger = 1' Stamp QR code to (100, 150) of the pdf on page 1qr.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)
using IronQr;using System.Collections.Generic;// Create a QR code objectQrCode 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-4qr.StampToExistingPdfPages(filepath, x, y, pages);
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);
ImportsIronQrImportsSystem.Collections.Generic' Create a QR code objectPrivate qr AsQrCode = QrWriter.Write("hello world")Private filepath AsString = "example.pdf"Private x AsInteger = 100Private y AsInteger = 150Private pages As New List(OfInteger)()pages.Add(1)pages.Add(2)pages.Add(3)pages.Add(4)' Stamp QR code to (100, 150) of the pdf on pages 1-4qr.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)
两个示例的输出
QR 代码选项
IronQR 提供广泛的自定义选项,可对 QR 代码行为和功能进行微调。 QrOptions 类提供了多个参数,如版本控制、编码类型、字符编码和错误纠正级别。 让我们来详细探讨一下这些选项。
using IronQr;using IronSoftware.Drawing;QrOptions options = new QrOptions{ // Change encoding to micro QR codeEncoding = IronQr.Enum.QrEncoding.MicroQRCode,};// Create QR codeQrCode qr = QrWriter.Write("1234", options);// Save QR code as a bitmapAnyBitmap qrImage = qr.Save();// Save QR code bitmap as fileqrImage.SaveAs("qrImage.png");
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");
ImportsIronQrImportsIronSoftware.DrawingPrivate options As New QrOptionsWith {.Encoding = IronQr.Enum.QrEncoding.MicroQRCode}' Create QR codePrivate qr AsQrCode = QrWriter.Write("1234", options)' Save QR code as a bitmapPrivate qrImage AsAnyBitmap = qr.Save()' Save QR code bitmap as fileqrImage.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")
错误修正
IronQR 采用标准 QR 纠错技术,确保生成的所有 QR 代码即使在恶劣条件下也具有容错性和可靠性。 此外,IronQR 允许您完全控制纠错级别,以便进一步微调。
提供 QrErrorCorrectionLevel 的有四个错误纠正级别:
Highest:30%错误纠正
High:25%错误纠正
Medium:15%错误纠正
Low:7%错误纠正
using IronQr;using IronSoftware.Drawing;QrOptions options = new QrOptions{ // Change error correction level to mediumErrorCorrectionLevel = QrErrorCorrectionLevel.Medium,};// Create QR codeQrCode qr = QrWriter.Write("1234", options);// Save QR code as a bitmapAnyBitmap qrImage = qr.Save();// Save QR code bitmap as fileqrImage.SaveAs("qrImage.png");
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");
ImportsIronQrImportsIronSoftware.DrawingPrivate options As New QrOptionsWith {.ErrorCorrectionLevel = QrErrorCorrectionLevel.Medium}' Create QR codePrivate qr AsQrCode = QrWriter.Write("1234", options)' Save QR code as a bitmapPrivate qrImage AsAnyBitmap = qr.Save()' Save QR code bitmap as fileqrImage.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")
您可以调整二维码版本,以存储更多数据。 高版本是库存或物流的理想选择,而低版本则适用于较小的数据,如短 URL。 只需更改 QrOptions 对象中的 Version 属性,并将其传递给 Write 方法即可根据需要生成QR码。
using IronQr;using IronSoftware.Drawing;QrOptions options = new QrOptions{ // Change QR code version to 40Version = 40,};// Create QR codeQrCode qr = QrWriter.Write("1234", options);// Save QR code as a bitmapAnyBitmap qrImage = qr.Save();// Save QR code bitmap as fileqrImage.SaveAs("qrImage.png");
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");
ImportsIronQrImportsIronSoftware.DrawingPrivate options As New QrOptionsWith {.Version = 40}' Create QR codePrivate qr AsQrCode = QrWriter.Write("1234", options)' Save QR code as a bitmapPrivate qrImage AsAnyBitmap = qr.Save()' Save QR code bitmap as fileqrImage.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")
从输出结果中可以看出,与第 5 版相比,第 40 版的二维码非常复杂和密集。
低版本需要更精确的扫描,没有更高分辨率的扫描仪可能难以扫描。 不过,更高的版本更容易扫描,即使使用较低分辨率的相机也是如此。 有关根据容量选择 QR 版本的更详细指南,请参阅 QR 版本列表。
字符编码
该选项决定了 QR 代码的编码方式。 在我们的示例中,我们将其改为 "UTF-32",而默认字符编码为 "ISO-8859-1"。
using IronQr;using IronSoftware.Drawing;QrOptions options = new QrOptions{ // Change character encoding to UTF-32CharacterEncoding = "UTF-32"};// Create QR codeQrCode qr = QrWriter.Write("1234", options);// Save QR code as a bitmapAnyBitmap qrImage = qr.Save();// Save QR code bitmap as fileqrImage.SaveAs("qrImage.png");
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");
ImportsIronQrImportsIronSoftware.DrawingPrivate options As New QrOptionsWith {.CharacterEncoding = "UTF-32"}' Create QR codePrivate qr AsQrCode = QrWriter.Write("1234", options)' Save QR code as a bitmapPrivate qrImage AsAnyBitmap = qr.Save()' Save QR code bitmap as fileqrImage.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")
QR 代码样式
除了简单易用的方法和处理输入数据的灵活性,IronQR 还提供了许多自定义和风格化 QR 代码的选项,使其独一无二。 QrStyleOptions 类提供了用于自定义QR码各个方面的各种参数。 让我们来探讨一下可用的选项。
using IronQr;using IronSoftware.Drawing;QrStyleOptions styleOptions = new QrStyleOptions(){ // Change the dimensions to 600pxDimensions = 600,};string url = "https://ironsoftware.com/csharp/qr/";// Create QR codeQrCode qr = QrWriter.Write(url);// Save QR code as a bitmapAnyBitmap qrImage = qr.Save(styleOptions);// Save QR code bitmap as fileqrImage.SaveAs("qrURLResized.png");
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");
ImportsIronQrImportsIronSoftware.DrawingPrivate styleOptions As New QrStyleOptions() With {.Dimensions = 600}Private url AsString = "https://ironsoftware.com/csharp/qr/"' Create QR codePrivate qr AsQrCode = QrWriter.Write(url)' Save QR code as a bitmapPrivate qrImage AsAnyBitmap = qr.Save(styleOptions)' Save QR code bitmap as fileqrImage.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")
using IronQr;using IronSoftware.Drawing;QrStyleOptions styleOptions = new QrStyleOptions(){ // Change margins to 20pxMargins = 20};string url = "https://ironsoftware.com/csharp/qr/";// Create QR codeQrCode qr = QrWriter.Write(url);// Save QR code as a bitmapAnyBitmap qrImage = qr.Save(styleOptions);// Save QR code bitmap as fileqrImage.SaveAs("qrURLMarginMultiple.png");
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");
ImportsIronQrImportsIronSoftware.DrawingPrivate styleOptions As New QrStyleOptions() With {.Margins = 20}Private url AsString = "https://ironsoftware.com/csharp/qr/"' Create QR codePrivate qr AsQrCode = QrWriter.Write(url)' Save QR code as a bitmapPrivate qrImage AsAnyBitmap = qr.Save(styleOptions)' Save QR code bitmap as fileqrImage.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")
更改每页边距
IronQR 还允许用户为每一面指定不同的页边距,提供更精细的控制。
using IronQr;using IronSoftware.Drawing;QrStyleOptions styleOptions = new QrStyleOptions(){ // Change marginsMarginBottom = 30,MarginTop = 100,MarginRight = 40,MarginLeft = 20,};string url = "https://ironsoftware.com/csharp/qr/";// Create QR codeQrCode qr = QrWriter.Write(url);// Save QR code as a bitmapAnyBitmap qrImage = qr.Save(styleOptions);// Save QR code bitmap as fileqrImage.SaveAs("qrURLMarginMultiple.png");
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");
ImportsIronQrImportsIronSoftware.DrawingPrivate styleOptions As New QrStyleOptions() With { .MarginBottom = 30, .MarginTop = 100, .MarginRight = 40, .MarginLeft = 20}Private url AsString = "https://ironsoftware.com/csharp/qr/"' Create QR codePrivate qr AsQrCode = QrWriter.Write(url)' Save QR code as a bitmapPrivate qrImage AsAnyBitmap = qr.Save(styleOptions)' Save QR code bitmap as fileqrImage.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")
颜色
我们可以使用 QrStyleOptions 类为QR码及其背景添加颜色。 自定义颜色可以使 QR 代码更加独特和醒目。 您可以使用 Color 和 BackgroundColor 属性更改颜色。 请确保导入 IronSoftware.Drawing 以获取可分配色的列表。
using IronQr;using IronSoftware.Drawing;// Load new logo imageAnyBitmap logo = AnyBitmap.FromFile("sample.png");// Add new logo to QR code style optionsQrStyleOptions styleOptions = new QrStyleOptions(){Logo = new QrLogo(logo, 50, 50, 10),};string url = "https://ironsoftware.com/csharp/qr/";// Create QR codeQrCode qr = QrWriter.Write(url);// Save QR code as a bitmapAnyBitmap qrImage = qr.Save(styleOptions);// Save QR code bitmap as fileqrImage.SaveAs("qrURLColored.png");
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");
ImportsIronQrImportsIronSoftware.Drawing' Load new logo imageDim logo AsAnyBitmap = AnyBitmap.FromFile("sample.png")' Add new logo to QR code style optionsDim styleOptions As New QrStyleOptions() With { .Logo = New QrLogo(logo, 50, 50, 10)}Dim url AsString = "https://ironsoftware.com/csharp/qr/"' Create QR codeDim qr AsQrCode = QrWriter.Write(url)' Save QR code as a bitmapDim qrImage AsAnyBitmap = qr.Save(styleOptions)' Save QR code bitmap as fileqrImage.SaveAs("qrURLColored.png")
Imports IronQr
Imports IronSoftware.Drawing
' Load new logo image
Dim logo As AnyBitmap = AnyBitmap.FromFile("sample.png")
' Add new logo to QR code style options
Dim styleOptions As New QrStyleOptions() With {
.Logo = New QrLogo(logo, 50, 50, 10)
}
Dim url As String = "https://ironsoftware.com/csharp/qr/"
' Create QR code
Dim qr As QrCode = QrWriter.Write(url)
' Save QR code as a bitmap
Dim qrImage As AnyBitmap = qr.Save(styleOptions)
' Save QR code bitmap as file
qrImage.SaveAs("qrURLColored.png")
添加徽标
除了颜色和尺寸,您还可以将公司徽标应用到 QR 代码中。 这有助于用户立即识别二维码并将其与您的品牌联系起来。 Logo 属性使您可以轻松地通过添加您的企业Logo来自定义QR码。
using IronQr;using IronSoftware.Drawing;// Load new logo imageAnyBitmap logo = AnyBitmap.FromFile("sample.png");// Add new logo to QR code style optionsQrStyleOptions styleOptions = new QrStyleOptions(){Logo = new QrLogo(logo, 50, 50, 10),};string url = "https://ironsoftware.com/csharp/qr/";// Create QR codeQrCode qr = QrWriter.Write(url);// Save QR code as a bitmapAnyBitmap qrImage = qr.Save(styleOptions);// Save QR code bitmap as fileqrImage.SaveAs("qrURLColored.png");
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");
ImportsIronQrImportsIronSoftware.Drawing' Load new logo imageDim logo AsAnyBitmap = AnyBitmap.FromFile("sample.png")' Add new logo to QR code style optionsDim styleOptions As New QrStyleOptions() With { .Logo = New QrLogo(logo, 50, 50, 10)}Dim url AsString = "https://ironsoftware.com/csharp/qr/"' Create QR codeDim qr AsQrCode = QrWriter.Write(url)' Save QR code as a bitmapDim qrImage AsAnyBitmap = qr.Save(styleOptions)' Save QR code bitmap as fileqrImage.SaveAs("qrURLColored.png")
Imports IronQr
Imports IronSoftware.Drawing
' Load new logo image
Dim logo As AnyBitmap = AnyBitmap.FromFile("sample.png")
' Add new logo to QR code style options
Dim styleOptions As New QrStyleOptions() With {
.Logo = New QrLogo(logo, 50, 50, 10)
}
Dim url As String = "https://ironsoftware.com/csharp/qr/"
' Create QR code
Dim qr As QrCode = QrWriter.Write(url)
' Save QR code as a bitmap
Dim qrImage As AnyBitmap = qr.Save(styleOptions)
' Save QR code bitmap as file
qrImage.SaveAs("qrURLColored.png")
IronQrPdfPasswordException:IronQrException 的子类,当用户试图添加水印的PDF受密码保护且未提供密码或提供错误密码时发生此错误。 它还涵盖了其他与 PDF 相关的错误,例如当 PDF 无法打开时,如示例所示。
结论
IronQR 为在 .NET 应用程序中生成和定制 QR 代码提供了一套全面的方法。 凭借其强大的功能,开发人员可以轻松创建具有各种数据编码、视觉样式和纠错级别的 QR 代码。 该库支持多种输出格式,并可无缝集成到现有文档中,是任何二维码项目的通用工具。 无论您需要的是基本的 QR 码还是高级的品牌解决方案,IronQR 都能提供灵活性和功能性,高效满足您的需求。