Back to Qrcoder

QRCoder

README.md

1.8.015.9 KB
Original Source

QRCoder

QRCoder is a simple C# library originally created by Raffael Herrmann for generating QR codes and Micro QR codes.

✨ Features

  • 🚀 Zero dependencies - No external libraries required (framework dependencies only)
  • Fast performance - Optimized QR code generation with low memory footprint
  • 🎨 Multiple output formats - PNG, SVG, PDF, ASCII, Bitmap, PostScript, and more
  • 📱 23+ payload generators - WiFi, vCard, URLs, payments, and many more
  • 🔧 Highly configurable - Error correction levels, custom colors, logos, and styling
  • 🌐 Cross-platform - Supports .NET 5+, .NET Framework 3.5+, .NET Core 1.0+, and .NET Standard 1.3+
  • 📦 Micro QR codes - Smaller QR codes for space-constrained applications

📦 Installation

Install via NuGet Package Manager:

bash
PM> Install-Package QRCoder

🚀 Quick Start

Generate a QR code with just a few lines of code, either using a renderer's static helper method, or by creating a QR code first and then passing it to a renderer:

csharp
using QRCoder;

// Generate a simple black and white PNG QR code
byte[] qrCodeImage = PngByteQRCodeHelper.GetQRCode("Hello World", QRCodeGenerator.ECCLevel.Q, 20);

// Generate a scalable black and white SVG QR code
using var qrCodeData = QRCodeGenerator.GenerateQrCode("Hello World", QRCodeGenerator.ECCLevel.Q);
using var svgRenderer = new SvgQRCode(qrCodeData);
string svg = svgRenderer.GetGraphic();

For more examples and detailed usage instructions, see: Wiki: How to use QRCoder

📱 Payload Generators

QR codes can encode structured data that triggers specific actions when scanned (e.g., WiFi credentials, contact information, URLs). QRCoder includes payload generators that create properly formatted strings for these common use cases.

Usage Example

csharp
using QRCoder;

// Create a bookmark payload
var bookmarkPayload = new PayloadGenerator.Bookmark("https://github.com/Shane32/QRCoder", "QRCoder Repository");

// Generate the QR code data from the payload
using var qrCodeData = QRCodeGenerator.GenerateQrCode(bookmarkPayload);

// Or override the ECC level
using var qrCodeData2 = QRCodeGenerator.GenerateQrCode(bookmarkPayload, QRCodeGenerator.ECCLevel.H);

// Render the QR code
using var pngRenderer = new PngByteQRCode(qrCodeData);
byte[] qrCodeImage = pngRenderer.GetGraphic(20);

Available Payload Types

Payload TypeUsage ExampleDescription
WiFinew PayloadGenerator.WiFi(ssid, password, auth)WiFi network credentials
URLnew PayloadGenerator.Url("https://example.com")Website URL
Bookmarknew PayloadGenerator.Bookmark(url, title)Browser bookmark
Mailnew PayloadGenerator.Mail(email, subject, body)Email with pre-filled fields
SMSnew PayloadGenerator.SMS(number, message)SMS message
MMSnew PayloadGenerator.MMS(number, subject)MMS message
Geolocationnew PayloadGenerator.Geolocation(lat, lng)GPS coordinates
PhoneNumbernew PayloadGenerator.PhoneNumber(number)Phone number for calling
SkypeCallnew PayloadGenerator.SkypeCall(username)Skype call
WhatsAppMessagenew PayloadGenerator.WhatsAppMessage(number, msg)WhatsApp message
ContactDatanew PayloadGenerator.ContactData(...)vCard/MeCard contact
CalendarEventnew PayloadGenerator.CalendarEvent(...)iCal/vEvent
OneTimePasswordnew PayloadGenerator.OneTimePassword(...)TOTP/HOTP for 2FA
BitcoinAddressnew PayloadGenerator.BitcoinAddress(address)Bitcoin payment
BitcoinCashAddressnew PayloadGenerator.BitcoinCashAddress(address)Bitcoin Cash payment
LitecoinAddressnew PayloadGenerator.LitecoinAddress(address)Litecoin payment
MoneroTransactionnew PayloadGenerator.MoneroTransaction(...)Monero payment
SwissQrCodenew PayloadGenerator.SwissQrCode(...)Swiss QR bill (ISO-20022)
Girocodenew PayloadGenerator.Girocode(...)SEPA payment (EPC QR)
BezahlCodenew PayloadGenerator.BezahlCode(...)German payment code
RussiaPaymentOrdernew PayloadGenerator.RussiaPaymentOrder(...)Russian payment (ГОСТ Р 56042-2014)
SlovenianUpnQrnew PayloadGenerator.SlovenianUpnQr(...)Slovenian UPN payment
ShadowSocksConfignew PayloadGenerator.ShadowSocksConfig(...)Shadowsocks proxy config

For detailed information about payload generators, see: Wiki: Advanced usage - Payload generators

🎨 QR Code Renderers

QRCoder provides multiple renderers for different output formats and use cases. Each renderer has specific capabilities and framework requirements.

RendererOutput FormatRequiresUsage Example
PngByteQRCodePNG byte arraynew PngByteQRCode(data).GetGraphic(20)
SvgQRCodeSVG stringnew SvgQRCode(data).GetGraphic(20)
QRCodeSystem.Drawing.BitmapWindows¹new QRCode(data).GetGraphic(20)
ArtQRCodeArtistic bitmap with custom imagesWindows¹new ArtQRCode(data).GetGraphic(20)
AsciiQRCodeASCII art stringnew AsciiQRCode(data).GetGraphic(1) or new AsciiQRCode(data).GetGraphicSmall()
Base64QRCodeBase64 encoded imagenew Base64QRCode(data).GetGraphic(20)
BitmapByteQRCodeBMP byte arraynew BitmapByteQRCode(data).GetGraphic(20)
PdfByteQRCodePDF byte arraynew PdfByteQRCode(data).GetGraphic(20)
PostscriptQRCodePostScript/EPS stringnew PostscriptQRCode(data).GetGraphic(20)
XamlQRCodeXAML DrawingImageXAML²new XamlQRCode(data).GetGraphic(20)
UnityQRCodeUnity Texture2DUnity³new UnityQRCode(data).GetGraphic(20)

Notes:

  • ¹ Requires Windows or System.Drawing.Common package (uses GDI+)
  • ² Requires the QRCoder.Xaml package
  • ³ Requires the QRCoder.Unity package

Framework Compatibility: Not all renderers are available on all target frameworks. Check the compatibility table for details.

For comprehensive information about renderers, see: Wiki: Advanced usage - QR Code renderers

🔧 Advanced Features

Micro QR Codes

QRCoder supports Micro QR codes, which are smaller versions of standard QR codes suitable for applications with limited space. Micro QR codes have significantly limited storage capacity—as few as 5 numeric digits (M1) or as many as 35 numeric digits (M4), with alphanumeric and byte data storing considerably less.

csharp
using QRCoder;

// Generate a Micro QR code (versions M1-M4, represented as -1 to -4)
using var qrCodeData = QRCodeGenerator.GenerateMicroQrCode("Hello", QRCodeGenerator.ECCLevel.L, requestedVersion: -2);
using var qrCode = new PngByteQRCode(qrCodeData);
byte[] qrCodeImage = qrCode.GetGraphic(20);

Note: Micro QR codes have limitations on data capacity and error correction levels. They support versions M1 through M4 (specified as -1 to -4), and not all ECC levels are available for all versions. M1 only supports detection (no ECC), M2 and M3 support L and M levels, and M4 supports L, M, and Q levels. For detailed capacity tables, see the Micro QR Code specification.

Working with QRCodeData

QRCodeData is the core data structure that represents a QR code's module matrix. It contains a List<BitArray> called ModuleMatrix, where each BitArray represents a row of modules in the QR code. A module is set to true for dark/black modules and false for light/white modules.

You can access the ModuleMatrix directly to read or manipulate the QR code data at the module level. This is useful for custom rendering implementations or analyzing QR code structure.

csharp
using QRCoder;

// Generate QR code data
using var qrCodeData = QRCodeGenerator.GenerateQrCode("Hello World", QRCodeGenerator.ECCLevel.Q);

// Access the module matrix
var moduleMatrix = qrCodeData.ModuleMatrix;
int size = moduleMatrix.Count; // Size of the QR code (includes quiet zone)

// Manually render as ASCII (versus the included ASCII renderer)
for (int y = 0; y < size; y++)
{
    for (int x = 0; x < size; x++)
    {
        // Check if module is dark (true) or light (false)
        bool isDark = moduleMatrix[y][x];
        Console.Write(isDark ? "██" : "  ");
    }
    Console.WriteLine();
}

⚠️ Troubleshooting

System.Drawing.Common Warnings (QRCode and ArtQRCode renderers)

The QRCode and ArtQRCode renderers depend on System.Drawing.Common, which Microsoft has removed cross-platform support for in .NET 6+. You may encounter one of the following build or runtime errors:

CA1416: This call site is reachable on all platforms. 'QRCode.QRCode(QRCodeData)' is only supported on: 'windows'

System.TypeInitializationException: The type initializer for 'Gdip' threw an exception.

System.PlatformNotSupportedException: System.Drawing.Common is not supported on this platform.

Solutions include:

  1. Use Windows-specific TFMs such as <TargetFramework>net8.0-windows</TargetFramework>
  2. Mark methods with the [SupportedOSPlatform("windows")] attribute
  3. Add platform guards by wrapping code with #if WINDOWS or if (OperatingSystem.IsWindows())
  4. Use cross-platform renderers such as PngByteQRCode, SvgQRCode, or BitmapByteQRCode

ISO-8859-2 Encoding Support (.NET Core and .NET 5+)

ISO-8859-2 encoding is not natively supported on .NET Core and .NET 5+. If you need to use ISO-8859-2 encoding in your code, you must:

  1. Install the System.Text.Encoding.CodePages NuGet package
  2. Register the encoding provider in your application startup code:
csharp
using System.Text;

// Register the code pages encoding provider
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

Note that the RussiaPaymentOrder payload generator already includes this registration internally, so no additional setup is required when using that class.

🚀 CI Builds

The NuGet feed contains only major/stable releases. If you want the latest functions and features, you can use the CI builds via Github packages.

(More information on how to use Github Packages in Nuget Package Manager can be found here.)

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

📄 License

QRCoder is a project originally by Raffael Herrmann and was first released in 10/2013. It's licensed under the MIT license.

Since 2025, QRCoder has been maintained by Shane32 with contributions from the community.

🙏 Credits

Glory to Jehovah, Lord of Lords and King of Kings, creator of Heaven and Earth, who through his Son Jesus Christ, has redeemed me to become a child of God. -Shane32