如何实现自定义二维码颜色
二维码已从简单的黑白图案演变为强大的品牌推广工具。 到 2025 年,企业已认识到,设计精良的二维码既能强化品牌形象,又能确保完全可扫描。 自定义二维码的前景色和背景色,可帮助您打造视觉上引人注目的设计。
IronQR 通过 QrStyleOptions 类,使修改二维码颜色变得简单。 您可以更改前景色(深色模块)、背景色或两者,以符合您的品牌规范。
在本操作指南中,我们将逐步介绍如何使用 IronQR 在 C# 中自定义二维码颜色。
快速入门:自定义二维码颜色
通过 QrStyleOptions 设置前景色和背景色,并保存样式化的二维码。
-
使用 NuGet 包管理器安装 https://www.nuget.org/packages/IronQR
PM > Install-Package IronQR -
复制并运行这段代码。
var qrCode = QrWriter.Write("https://example.com"); var style = new QrStyleOptions { Color = Color.DarkBlue, BackgroundColor = Color.LightYellow }; qrCode.Save(style).SaveAs("colored-qr.png"); -
部署到您的生产环境中进行测试
通过免费试用立即在您的项目中开始使用IronQR
最小工作流程(5 个步骤)
- 下载 C# 库,创建自定义颜色的二维码
- 使用 `QrWriter` 类生成二维码
- 初始化 `QrStyleOptions` 以配置外观
- 设置 `Color` 和 `BackgroundColor` 属性
- 使用
SaveAs保存格式化的二维码
二维码生成时更改背景颜色
二维码的背景色是指围绕深色模块的浅色区域。 默认颜色为白色,但您可以将其更改为与前景色形成足够对比的任意颜色。
设置自定义背景颜色在将二维码放置于彩色表面时,或希望与品牌配色方案相匹配时非常有用。 请确保代码的对比度足够高,以便扫描仪能够可靠地读取代码。
:path=/static-assets/qr/content-code-examples/how-to/implement-custom-qr-code-background.cs
using IronQr;
using IronSoftware.Drawing;
// Create a QR code
QrCode qr = QrWriter.Write("https://ironsoftware.com/csharp/qr/");
// Set background color
QrStyleOptions styleOptions = new QrStyleOptions()
{
BackgroundColor = Color.LightBlue
};
// Save QR code with custom background
AnyBitmap qrImage = qr.Save(styleOptions);
qrImage.SaveAs("qrBackgroundColor.png");
Imports IronQr
Imports IronSoftware.Drawing
' Create a QR code
Dim qr As QrCode = QrWriter.Write("https://ironsoftware.com/csharp/qr/")
' Set background color
Dim styleOptions As New QrStyleOptions() With {
.BackgroundColor = Color.LightBlue
}
' Save QR code with custom background
Dim qrImage As AnyBitmap = qr.Save(styleOptions)
qrImage.SaveAs("qrBackgroundColor.png")
更改前景色
前景色代表二维码中的深色模块——即扫描仪实际读取的数据图案。 虽然黑色是标准选择,但您也可以使用任何与背景形成良好对比的深色。
更改前景色可让您将品牌的主色调融入二维码设计中。 深蓝色、深绿色或浓郁的酒红色都是黑色之外的不错选择。
:path=/static-assets/qr/content-code-examples/how-to/implement-custom-qr-code-foreground.cs
using IronQr;
using IronSoftware.Drawing;
// Create a QR code
QrCode qr = QrWriter.Write("https://ironsoftware.com/csharp/qr/");
// Set background color
QrStyleOptions styleOptions = new QrStyleOptions()
{
Color = Color.PaleVioletRed
};
// Save QR code with custom background
AnyBitmap qrImage = qr.Save(styleOptions);
qrImage.SaveAs("qrBackgroundColor.png");
Imports IronQr
Imports IronSoftware.Drawing
' Create a QR code
Dim qr As QrCode = QrWriter.Write("https://ironsoftware.com/csharp/qr/")
' Set background color
Dim styleOptions As New QrStyleOptions() With {
.Color = Color.PaleVioletRed
}
' Save QR code with custom background
Dim qrImage As AnyBitmap = qr.Save(styleOptions)
qrImage.SaveAs("qrBackgroundColor.png")
结论
使用 IronQR 自定义二维码颜色,既能激发创意,又能确保二维码完全正常使用:
- 背景颜色:使用
BackgroundColor更改二维码图案后方的浅色区域 - 前景色:使用
Color来修改用于编码数据的深色模块 - 统一样式:同时设置这两个属性,以实现品牌风格的完全统一
请注意保持前景色与背景色之间的适当对比度,以确保在不同设备和光照条件下都能清晰辨识。

