How to Generate a QR Code in C#?

How to Generate a QR Code in C#?

QR codes are everywhere—from payment systems to product packaging. Whether you’re developing an inventory system, a ticketing platform, or just need a quick way to encode data, generating QR codes in C# is straightforward with the IronQR library.

In this article, we’ll explore how to generate high-quality QR codes in C# using IronQR, a robust and easy-to-use library for QR code generation.

Why Use IronQR for QR Code Generation?

IronQR simplifies QR code generation with:

? Quick installation

? High customization (color, size, logo embedding, etc.)

? Error correction for readability

? Support for multiple formats (PNG, JPG, SVG, PDF, etc.)

? Cross-platform compatibility across .NET Framework and supported target frameworks (Windows, Linux, macOS, Azure, AWS, etc.).

Now, let’s dive into the implementation.

Step 1: Install IronQR

First, create or open an existing project in Visual Studio. Then install the QR Code library via NuGet Package Manager.

Install-Package IronQR        


Install IronQR - Nuget Package Manager Console

Step 2: Generate a Basic QR Code

Here’s how you can generate your first QR code with IronQR in C#:

using IronQr;
using IronSoftware.Drawing;
 

QrCode myQr = QrWriter.Write("hello My QR!");
AnyBitmap qrImage = myQr.Save();
qrImage.SaveAs("qr.png");        

Explanation:

  1. QrWriter.Write("hello My QR!") – This creates a QR code containing the text "hello My QR!".
  2. myQr.Save() – This saves the QR code as an AnyBitmap object, which allows further manipulation.
  3. qrImage.SaveAs("qr.png") – This saves the generated QR code image as a PNG file named "qr.png".

This simple approach lets you quickly generate and save QR codes using the IronQR library in C#.


QR Code - Hello My QR

The C# QR code generator simplifies the process of creating QR codes for various applications, ensuring efficiency and customization.

Step 3: Customize Your QR Code

You can customize the QR code’s color, size, and error correction level as shown below:

using IronQr;
using IronSoftware.Drawing;
 

QrOptions options = new QrOptions(IronQr.QrErrorCorrectionLevel.High, 20);
 QrCode myQr = QrWriter.Write("Hello My QR!", options);
 AnyBitmap logoBmp = new AnyBitmap("logo.png");

 QrStyleOptions style = new QrStyleOptions
 {
     Dimensions = 300, // px
     Margins = 10, // px
     Color = Color.Gray,
     Logo = new QrLogo
     {
         Bitmap = logoBmp,
         Width = 100,
         Height = 100,
         CornerRadius = 2
     }
 };

 AnyBitmap qrImage = myQr.Save(style);
 qrImage.SaveAs("qr_styled.png");        

Explanation:

  1. QrOptions options = new QrOptions(IronQr.QrErrorCorrectionLevel.High, 20);
  2. QrWriter.Write("Hello My QR!", options);
  3. AnyBitmap logoBmp = new AnyBitmap("logo.png");
  4. QrStyleOptions style = new QrStyleOptions { ... }
  5. AnyBitmap qrImage = myQr.Save(style);
  6. qrImage.SaveAs("qr_styled.png");

This approach creates a well-styled QR code with a custom color, embedded logo, and high error correction, ensuring better readability and design flexibility.


Advance QR - IronQR

Step 4: Read QR Codes in C#

IronQR can also read QR codes from images or screenshots:

using IronQr;
using IronSoftware.Drawing; 


var inputBmp = AnyBitmap.FromFile("qr.png");
 QrImageInput imageInput = new QrImageInput(inputBmp);
 QrReader reader = new QrReader();
 IEnumerable<QrResult> results = reader.Read(imageInput);
 foreach (QrResult result in results)
     Console.WriteLine(result.Value);        

Explanation:

  1. AnyBitmap.FromFile("qr.png");
  2. QrImageInput imageInput = new QrImageInput(inputBmp);
  3. QrReader reader = new QrReader();
  4. IEnumerable<QrResult> results = reader.Read(imageInput);
  5. foreach (QrResult result in results) Console.WriteLine(result.Value);

This approach enables reading and extracting information from QR codes in images, making it useful for QR code scanning and processing applications.

Read QR

Conclusion

Generating QR codes in C# is incredibly simple with IronQR. Whether you need a basic QR code, a customized version with branding, or advanced error correction, IronQR provides a powerful yet user-friendly solution.

?? Key Takeaways:

? Easy installation and usage

? Customization (size, color, logo, error correction, etc.)

? Supports multiple formats (PNG, JPG, SVG, PDF, etc.)

? Ability to read QR codes as well


?? Try IronQR today and enhance your C# applications with dynamic QR code generation!

要查看或添加评论,请登录

社区洞察

其他会员也浏览了