条形码,特别是二维矩阵条形码,如二维码,现在很常见。所以 C# 开发者经常会质疑如何在 C# 应用程序中生成二维码。这可以通过使用 Aspose.BarCode for .NET 的几行代码快速完成。
您可以使用此答案中给出的步骤在 .NET 应用程序(包括 Windows 应用程序或使用 C# 的 Web 应用程序)中轻松创建条形码或 QR 码。
在 C# 中生成二维码的步骤
- 首先需要 Aspose.BarCode for .NET 的 Nuget 包
- 接下来,使用 Aspose.Barcode 和 Aspose.BarCode.Generation 命名空间
- 使用 SetLicense 方法添加 Aspose 许可证
- 现在,创建一个 BarCodeGenerator Class 的实例并将 QR 设置为编码类型
- 设置要添加到二维码的代码文本
- 设置 DocumentBuilder 对象的必需属性
- 最后一步,将二维码另存为图片格式
在此示例中,我们将 QR 码图像保存为 PNG,但是,您可以将 QR 码保存为 Bmp、Gif、Jpeg、PNG、Tiff、TiffInCmyk、EMF 或 SVG image formats。您只需要在保存生成的 QR code 的同时传递图像格式。
在 C# 中生成二维码的示例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
//Add reference to Aspose.BarCode API | |
//Use following two namespaces to run QR generator code | |
using Aspose.BarCode; | |
using Aspose.BarCode.Generation; | |
namespace GenerateQRCode | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Set Aspose license before generating QR code using Aspose.Barcode | |
Aspose.BarCode.License AsposeBarCodeLicense = new Aspose.BarCode.License(); | |
AsposeBarCodeLicense.SetLicense(@"c:\asposelicense\license.lic"); | |
//Create an instance of BarCode generator class | |
//specify Barcode Encode Type as QR | |
BarcodeGenerator QRCodeGenerator = new BarcodeGenerator(EncodeTypes.QR); | |
//set text to be encoded as generated QR code | |
QRCodeGenerator.CodeText = "Text To Encode"; | |
//Save the generated QR code based barcode in image format | |
//Aspose.Barcode support multiple image formats when saving generated QR code | |
QRCodeGenerator.Save("Generated_QR_Code.png", BarCodeImageFormat.Png); | |
} | |
} | |
} |
如果您正在开发自己的 QR 码生成器或在您的应用程序中添加此 QR 生成功能,上面给出的步骤和示例可以为您节省大量时间和精力。