QRCoder Documentation

repository·master·Indexed 26 days ago

https://github.com/shane32/qrcoder

A lightweight, zero-dependency C# library for generating QR codes and Micro QR codes. It supports multiple output formats including PNG, SVG, PDF, ASCII, and XAML (via QRCoder.Xaml), as well as structured payload generators for WiFi, payments, URLs, and more. The library provides a QRCodeGenerator for creating QRCodeData and various renderers to convert that data into graphics.

Tokens
3.6K
Snippets
11
Records
14
Agent score
84%

What's inside QRCoder

  1. Quick Start: Generate QR codes

    master

    You can generate QR codes using two primary patterns:

    1. Using a renderer's static helper method (for simple tasks like PNG generation).
    2. Creating QRCodeData first, then passing it to a specific renderer (for more control or different formats).

    Note: QRCodeGenerator.ECCLevel defines the error correction level (e.g., Q).

    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();
  2. Use XamlQRCode to render QR codes in WPF/XAML

    master

    QRCoder.Xaml is an extension for the QRCoder.NET library that provides the XamlQRCode renderer. It allows you to generate QR codes as DrawingImage objects, which are suitable for use in WPF or other XAML-based projects.

    To use it, you must first generate QRCodeData using a QRCodeGenerator, then pass that data to a XamlQRCode instance to retrieve the graphic.

    using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
    using (QRCodeData qrCodeData = qrGenerator.CreateQrCode("The text which should be encoded.", eccLevel))
    using (XamlQRCode qrCode = new XamlQRCode(qrCodeData))
    {
        DrawingImage qrCodeAsXaml = qrCode.GetGraphic(20);
    }
  3. Quick Start: Generate a scalable black and white SVG QR code

    master

    To generate a scalable SVG, first generate the QRCodeData using QRCodeGenerator.GenerateQrCode, then pass that data to an SvgQRCode renderer and call GetGraphic().

    // 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();
  4. Quick Start: Generate a simple black and white PNG QR code

    master

    Use PngByteQRCodeHelper.GetQRCode to quickly generate a PNG byte array for a QR code. You must specify the payload string, the Error Correction Code (ECC) level, and the pixel size.

    // Generate a simple black and white PNG QR code
    byte[] qrCodeImage = PngByteQRCodeHelper.GetQRCode("Hello World", QRCodeGenerator.ECCLevel.Q, 20);
  5. Resolve System.Drawing.Common cross-platform issues

    master

    The QRCode and ArtQRCode renderers depend on System.Drawing.Common, which is only supported on Windows in .NET 6+. If you encounter CA1416, System.TypeInitializationException (Gdip), or System.PlatformNotSupportedException, use one of these solutions:

    1. Use Windows-specific TFMs: Set <TargetFramework>net8.0-windows</TargetFramework> in your project file.
    2. Use Attributes: Mark methods with [SupportedOSPlatform("windows")].
    3. Use Platform Guards: Wrap code with #if WINDOWS or if (OperatingSystem.IsWindows()).
    4. Use Cross-Platform Renderers: Switch to PngByteQRCode, SvgQRCode, or BitmapByteQRCode to avoid System.Drawing.Common entirely.
  6. Enable ISO-8859-2 encoding in .NET Core and .NET 5+

    master

    ISO-8859-2 encoding is not natively supported in .NET Core and .NET 5+. To use it, you must install the System.Text.Encoding.CodePages NuGet package and register the provider at application startup:

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

    Note: The RussiaPaymentOrder payload generator handles this registration internally.

  7. Use Payload Generators for structured data

    master

    Instead of manually formatting strings for WiFi, URLs, or contact info, use PayloadGenerator classes to create properly formatted payloads. These payloads can then be passed directly into QRCodeGenerator.GenerateQrCode().

    Example: Creating a Bookmark Payload

    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);
  8. Generate Micro QR Codes

    master

    QRCoder supports Micro QR codes, which are smaller versions of standard QR codes. They have limited storage capacity (e.g., M1 supports as few as 5 numeric digits, M4 up to 35).

    To generate a Micro QR code, use QRCodeGenerator.GenerateMicroQrCode. The requestedVersion parameter uses integers from -1 to -4 to represent versions M1 through M4 respectively.

    Version and ECC Support:

    • M1 (-1): Detection only (no ECC).
    • M2 (-2) & M3 (-3): Support ECCLevel.L and ECCLevel.M.
    • M4 (-4): Supports ECCLevel.L, ECCLevel.M, and ECCLevel.Q.
    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);
  9. Access and manipulate QRCodeData module matrix

    master

    The QRCodeData object contains the core module matrix of a QR code. You can access the ModuleMatrix property, which is a List<BitArray>. Each BitArray represents a row of modules where true indicates a dark/black module and false indicates a light/white module. This is useful for custom rendering or structural analysis.

    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();
    }
  10. Reference: Available Payload Types

    master

    QRCoder provides various payload generators to encode structured data. Common types include:

    Payload TypeUsage Example
    WiFinew PayloadGenerator.WiFi(ssid, password, auth)
    URLnew PayloadGenerator.Url("https://example.com")
    Bookmarknew PayloadGenerator.Bookmark(url, title)
    Mailnew PayloadGenerator.Mail(email, subject, body)
    SMSnew PayloadGenerator.SMS(number, message)
    MMSnew PayloadGenerator.MMS(number, subject)
    Geolocationnew PayloadGenerator.Geolocation(lat, lng)
    PhoneNumbernew PayloadGenerator.PhoneNumber(number)
    SkypeCallnew PayloadGenerator.SkypeCall(username)
    WhatsAppMessagenew PayloadGenerator.WhatsAppMessage(number, msg)
    ContactDatanew PayloadGenerator.ContactData(...)
    CalendarEventnew PayloadGenerator.CalendarEvent(...)
    OneTimePasswordnew PayloadGenerator.OneTimePassword(...)
    BitcoinAddressnew PayloadGenerator.BitcoinAddress(address)
    BitcoinCashAddressnew PayloadGenerator.BitcoinCashAddress(address)
    LitecoinAddressnew PayloadGenerator.LitecoinAddress(address)
    MoneroTransactionnew PayloadGenerator.MoneroTransaction(...)
    SwissQrCodenew PayloadGenerator.SwissQrCode(...)
    Girocodenew PayloadGenerator.Girocode(...)
    BezahlCodenew PayloadGenerator.BezahlCode(...)
    RussiaPaymentOrdernew PayloadGenerator.RussiaPaymentOrder(...)
    SlovenianUpnQrnew PayloadGenerator.SlovenianUpnQr(...)
    ShadowSocksConfignew PayloadGenerator.ShadowSocksConfig(...)
  11. Reference: QR Code Renderers

    master

    Choose a renderer based on your required output format and platform constraints.

    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 bitmapWindows¹new ArtQRCode(data).GetGraphic(20)
    AsciiQRCodeASCII art stringnew AsciiQRCode(data).GetGraphic(1)
    Base64QRCodeBase64 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.