如何实现自定义二维码颜色

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

二维码已从简单的黑白图案演变为强大的品牌推广工具。 到 2025 年,企业已认识到,设计精良的二维码既能强化品牌形象,又能确保完全可扫描。 自定义二维码的前景色和背景色,可帮助您打造视觉上引人注目的设计。

IronQR 通过 QrStyleOptions 类,使修改二维码颜色变得简单。 您可以更改前景色(深色模块)、背景色或两者,以符合您的品牌规范。

在本操作指南中,我们将逐步介绍如何使用 IronQR 在 C# 中自定义二维码颜色。

快速入门:自定义二维码颜色

通过 QrStyleOptions 设置前景色和背景色,并保存样式化的二维码。

  1. 使用 NuGet 包管理器安装 https://www.nuget.org/packages/IronQR

    PM > Install-Package IronQR
  2. 复制并运行这段代码。

    var qrCode = QrWriter.Write("https://example.com");
    var style = new QrStyleOptions { Color = Color.DarkBlue, BackgroundColor = Color.LightYellow };
    qrCode.Save(style).SaveAs("colored-qr.png");
  3. 部署到您的生产环境中进行测试

    通过免费试用立即在您的项目中开始使用IronQR

    arrow pointer

二维码生成时更改背景颜色

二维码的背景色是指围绕深色模块的浅色区域。 默认颜色为白色,但您可以将其更改为与前景色形成足够对比的任意颜色。

设置自定义背景颜色在将二维码放置于彩色表面时,或希望与品牌配色方案相匹配时非常有用。 请确保代码的对比度足够高,以便扫描仪能够可靠地读取代码。

: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")
$vbLabelText   $csharpLabel
带有自定义背景色的二维码

更改前景色

前景色代表二维码中的深色模块——即扫描仪实际读取的数据图案。 虽然黑色是标准选择,但您也可以使用任何与背景形成良好对比的深色。

更改前景色可让您将品牌的主色调融入二维码设计中。 深蓝色、深绿色或浓郁的酒红色都是黑色之外的不错选择。

: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")
$vbLabelText   $csharpLabel
带有自定义前景色调的二维码

结论

使用 IronQR 自定义二维码颜色,既能激发创意,又能确保二维码完全正常使用:

  • 背景颜色:使用 BackgroundColor 更改二维码图案后方的浅色区域
  • 前景色:使用 Color 来修改用于编码数据的深色模块
  • 统一样式:同时设置这两个属性,以实现品牌风格的完全统一

请注意保持前景色与背景色之间的适当对比度,以确保在不同设备和光照条件下都能清晰辨识。

如需了解包括徽标、边距和尺寸在内的更多样式选项,请访问 IronQR 文档,或在 GitHub 上查看更多代码示例

Ahmad Sohail
全栈开发者

Ahmad 是一名全栈开发人员,拥有扎实的 C#、Python 和 Web 技术基础。他对构建可扩展的软件解决方案深感兴趣,并喜欢探索设计和功能在实际应用中如何结合。

在加入 Iron Software 团队之前,Ahmad 致力于自动化项目和 API 集成,专注于提高性能和开发人员体验。

在业余时间,他喜欢尝试 UI/UX 想法,贡献开源工具,并偶尔从事技术写作和文档工作,以便让复杂主题更易于理解。

准备开始了吗?
Nuget 下载 63,625 | 版本: 2026.4 刚刚发布
Still Scrolling Icon

还在滚动吗?

想快速获得证据? PM > Install-Package IronQR
运行示例 观看您的 URL 变成 QR 代码。