QR Code Generator in C# (.NET) with the QRCoder Library
QR codes have become an integral part of the modern web and mobile applications. They are used for quickly sharing links, contact information, Wi-Fi passwords, payment details, and much more. In the C# (.NET) ecosystem, one of the most popular and easy-to-use libraries for generating QR codes is QRCoder. In this article, we will thoroughly cover how to install the library, create a simple and complex QR code, and save it in various formats (PNG, SVG).
What is QRCoder?
QRCoder is a lightweight, high-performance open-source library for .NET (supports .NET Framework 4.6+ and .NET Core/.NET 5+). It requires no external dependencies except the standard System.Drawing library for raster images. Key features:
- Generate QR codes from text, URLs, contacts (vCard), Wi-Fi settings, SMS, email, and other formats.
- Configure error correction level (L, M, Q, H).
- Select QR code version (matrix size).
- Export to PNG, BMP, SVG, XAML, EPS, PostScript, and HTML.
- Support for colors, background, transparency, and custom logos.
Installation in a C# Project
Installing the QRCoder library is done via the NuGet Package Manager. Open the Package Manager Console or use the .NET CLI:
dotnet add package QRCoderOr via the Package Manager Console:
Install-Package QRCoderBasic Example — Generating a Simple QR Code
Let's create a console application that generates a QR code for the string "https://pythonlib.ru" and saves it to a PNG file.
using QRCoder;using System.Drawing;
string url = "https://pythonlib.ru";QRCodeGenerator qrGenerator = new QRCodeGenerator();QRCodeData qrCodeData = qrGenerator.CreateQrCode(url, QRCodeGenerator.ECCLevel.Q);QRCode qrCode = new QRCode(qrCodeData);
// Generate image (pixels)Bitmap qrCodeImage = qrCode.GetGraphic(20);
// Save to fileqrCodeImage.Save("qrcode.png", System.Drawing.Imaging.ImageFormat.Png);
Console.WriteLine("QR code created: qrcode.png");In this example:
ECCLevel.Q— error correction level (25% of data can be damaged).GetGraphic(20)— size of one module (square) in pixels.- The result is a black-and-white PNG.
Customizing Appearance — Color, Logo, SVG
QRCoder allows flexible customization of the QR code's appearance. Let's look at an example with a colored background, dark modules, and an embedded logo.
using QRCoder;using System.Drawing;
string data = "https://pythonlib.ru";QRCodeGenerator qrGenerator = new QRCodeGenerator();QRCodeData qrCodeData = qrGenerator.CreateQrCode(data, QRCodeGenerator.ECCLevel.Q);QRCode qrCode = new QRCode(qrCodeData);
// Color settingsColor darkColor = Color.FromArgb(52, 73, 94); // dark blueColor lightColor = Color.FromArgb(236, 240, 241); // light gray
// Generate with logo (logo must be centered)Bitmap logo = new Bitmap("logo.png");Bitmap qrCodeImage = qrCode.GetGraphic(20, darkColor, lightColor, logo);
qrCodeImage.Save("qrcode_color.png", System.Drawing.Imaging.ImageFormat.Png);Console.WriteLine("Colored QR code with logo saved.");Note: for proper operation with a logo, it is recommended to use the error correction level ECCLevel.Q or ECCLevel.H, as the logo covers part of the data.
Export to SVG (Vector Format)
If you need a scalable QR code (e.g., for printing), use the SVG renderer. To do this, install the additional package QRCoder.Svg: