借助Aspose.BarCode条码控件,C# 中的文本转 QR 码生成器

二维码用于在较小的空间内存储大量数据。它们易于使用,可以通过智能手机或其他设备扫描来打开网站、观看视频或访问其他编码信息。在这篇博文中,我们将学习如何使用 C# 以编程方式生成基于文本的 QR 码。我们将提供分步指南和代码片段,帮助您使用 C# 开发自己的文本到 QR 码生成器。

Aspose.BarCode for .NET 是一个功能强大的API,可以从任意角度生成和识别多种图像类型的一维和二维条形码。开发人员可以轻松添加条形码生成和识别功能,以及在.NET应用程序中将生成的条形码导出为高质量的图像格式。 Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

Aspose.BarCode 最新下载icon-default.png?t=N7T8https://www.evget.com/product/576/download

C# 文本到 QR 码生成器 API

为了将任何文本转换为 QR 码,我们将使用Aspose.BarCode for .NET API。它允许在 .NET 应用程序中生成、扫描和读取条形码和 QR 码。该 API 允许开发人员轻松地将生成的条形码或 QR 码保存为各种图像格式,例如PNG、JPEG和TIFF。它支持 60 多种条形码符号,例如 QR 码、Code 128、Code 39、EAN、UPC 等。

请下载API的DLL或使用以下NuGet命令安装它:

PM> Install-Package Aspose.BarCode
在 C# 中将文本转换为 QR 码

我们可以按照以下步骤轻松地从任何提供的文本生成二维码:

  1. 创建BarcodeGenerator类的实例。
  2. EncodeTypes设置为QR
  3. 指定要编码的输入文本。
  4. (可选)指定 QR 码自定义选项。
  5. 最后,调用Save()方法将生成的二维码图像保存到给定的文件路径中。

以下代码示例展示了如何使用 C# 将文本转换为二维码

// This code example demonstartes how to generate a QR code from Text.
// Initialize the BarcodeGenerator
// Specify Encode type as QR
var generator = new BarcodeGenerator(EncodeTypes.QR);

// Specify code text to encode
generator.CodeText = "Your text goes here!";

// Specify the size of the image
generator.Parameters.Barcode.XDimension.Pixels = 8;
generator.Parameters.Resolution = 500;

// Save the generated QR code
generator.Save("C:\\Files\\Text_QR_Code.jpg");

C# 中的文本转二维码

在 C# 中自定义文本到 QR 代码的生成

我们可以按照前面提到的步骤自定义从文本生成的二维码的外观。然而,我们需要设置一些额外的属性来自定义背景颜色、边框颜色、图像高度等。

以下代码示例展示了如何在 C# 中自定义文本以生成 QR 码。

// This code example demonstartes how to generate a QR code from Text.
// Initialize the BarcodeGenerator
// Specify Encode type as QR
var generator = new BarcodeGenerator(EncodeTypes.QR);

// Specify code text to encode
generator.CodeText = "Your text goes here!";

// Specify the size of the image
generator.Parameters.Barcode.XDimension.Pixels = 8;

// Set background color
generator.Parameters.BackColor = Color.Red;

// Set QR color
generator.Parameters.Barcode.BarColor = Color.Blue;

// Set image resolution
generator.Parameters.Resolution = 300;

// Set border
generator.Parameters.Border.DashStyle = BorderDashStyle.Solid;
generator.Parameters.Border.Color = Color.Yellow;
generator.Parameters.Border.Visible = true;
generator.Parameters.Border.Width.Point = 4;

// Save the generated QR code
generator.Save("C:\\Files\\Customized_QR_Code.jpg");

在 C# 中自定义文本到 QR 代码的生成

用 C# 生成带文本的 QR 码

我们还可以将人类可读的文本添加到生成的二维码图像中。它可以是主文本标签、顶部标题或底部标题。我们可以按照前面提到的步骤添加代码文本和下面的标题。但是,我们只需要设置CodeTextParameters 和 CaptionBelow 属性,如下所示:

// This code example demonstartes how to generate a QR code from Text.
// Initialize the BarcodeGenerator
// Specify Encode type as QR
var generator = new BarcodeGenerator(EncodeTypes.QR);

// Specify code text to encode
generator.CodeText = "Your text goes here!";

// Specify the size of the image
generator.Parameters.Barcode.XDimension.Pixels = 8;

// Set background color
generator.Parameters.BackColor = Color.White;

// Set QR color
generator.Parameters.Barcode.BarColor = Color.Blue;

// Set image resolution
generator.Parameters.Resolution = 300;

// Set border
generator.Parameters.Border.DashStyle = BorderDashStyle.Solid;
generator.Parameters.Border.Color = Color.Red;
generator.Parameters.Border.Visible = true;
generator.Parameters.Border.Width.Point = 4;

// Show the code text above the QR code
generator.Parameters.Barcode.CodeTextParameters.TwoDDisplayText = "Aspose.BarCode";
generator.Parameters.Barcode.CodeTextParameters.Location = CodeLocation.Above;

// Set the caption below the QR code
generator.Parameters.Barcode.CodeTextParameters.TwoDDisplayText = "HELLO";
generator.Parameters.CaptionBelow.Text = "ASPOSE";
generator.Parameters.CaptionBelow.Visible = true;
generator.Parameters.CaptionBelow.Font.Style = FontStyle.Bold;
generator.Parameters.CaptionBelow.Font.Size.Pixels = 18;
generator.Parameters.CaptionBelow.Font.FamilyName = "Verdana";

// Save the generated QR code
generator.Save("C:\\Files\\QR_Code_with_text.jpg");

用 C# 生成带文本的 QR 码

您可以使用使用此 API 开发的在线QR 代码生成器 Web 应用程序,从在线输入的任何文本免费生成 QR 代码。

在这篇博文中,我们学习了如何使用 C# 开发文本到 QR 码生成器。我们还了解了如何自定义 QR 码并向生成的图像添加标题。

  • 31
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值