Using QRCoder in .NET 6.0 to Generate QRCode Pictures

QRCoder is a well-known QRCode generator for .NET. It has been around since the days of .NET Framework, and it supports many QRCode output formats, and it can output to Bitmap or PNG image types. However, I recently found that the latest version of v1.4.3 has some problems with .NET 6.0, so I’m here to document the correct way to use QRCoder.

QRCode

Install QRCoder package

Whether you are using the .NET Framework or .NET Core, you can install the QRCoder suite via NuGet, which supports multiple target architectures, of which only the net6.0 architecture has no dependencies!

  • PMC (Package Manager Console)

    Install-Package QRCoder
    
  • .NET CLI

    Install-Package QRCoder
    

Although there are no dependencies in the net6.0 architecture, this release will result in the complete loss of some of the early popular QRCode classes, which you will not find when developing in .NET 6.0+. Details of why can be found in Base64QRCode does not exist in 1.4.3 - Issue #361 - codebude/QRCoder.

Using the QRCoder in the .NET Framework

In the .NET Framework, you can generate QRCode images in several different ways.

  1. Generate images through the helper methods provided by the QRCodeHelper static class.

    var url = "https://blog.miniasp.com";
    Bitmap image = QRCodeHelper.GetQRCode(url, 10, Color.Black, Color.White, QRCodeGenerator.ECCLevel.Q);
    

    The magic variable 10 above is the pixel size of each module, the larger the number, the larger the resulting QR Code image.

  2. Generate images via the QRCodeGenerator, QRCodeData and QRCode classes.

    Bitmap image = null;
    
    using (QRCodeGenerator qRCodeGenerator = new QRCodeGenerator())
    {
        using (QRCodeData data = qRCodeGenerator.CreateQrCode(url, QRCodeGenerator.ECCLevel.Q))
        {
            using (QRCode qRCode = new QRCode(data))
            {
                image = qRCode.GetGraphic(10);
            }
        }
    };
    

    If you want to change the color of a QRCode picture, you can pass the Color structure through the GetGraphic() method of the QRCode class.

    Bitmap image = null;
    
    using (QRCodeGenerator qRCodeGenerator = new QRCodeGenerator())
    {
        using (QRCodeData data = qRCodeGenerator.CreateQrCode(url, QRCodeGenerator.ECCLevel.Q))
        {
            using (QRCode qRCode = new QRCode(data))
            {
                image = qRCode.GetGraphic(10, Color.Green, Color.White, drawQuietZones: true);
            }
        }
    };
    

Using QRCoder with .NET 6.0+

Since .NET removed most of the classes from the System.Drawing namespace, which of course included the Bitmap class, the GetGraphic() method of the above program won’t work, and you won’t even be able to find the QRCode class, as shown below.

Bitmap and QRCode classes could not be found.

There are two solutions:

  1. Use the PngByteQRCode class to generate images instead.

    var url = "https://blog.miniasp.com";
    
    using QRCodeGenerator qRCodeGenerator = new QRCodeGenerator();
    using QRCodeData data = qRCodeGenerator.CreateQrCode(url, QRCodeGenerator.ECCLevel.Q);
    using PngByteQRCode qRCode = new PngByteQRCode(data);
    byte[] image = qRCode.GetGraphic(10);
    

    If you want to change the color of a QR code image, in .NET, you can specify it using the CreateQrCode() method of the qRCode.GetGraphic() class. However, the syntax for specifying the color is different from the .NET Framework. Instead of passing a Color type, you need to input a byte[] where the content represents the color codes for the RGB (Red, Green, Blue) components.

    var url = "https://blog.miniasp.com";
    
    using QRCodeGenerator qRCodeGenerator = new QRCodeGenerator();
    using QRCodeData data = qRCodeGenerator.CreateQrCode(url, QRCodeGenerator.ECCLevel.Q);
    using PngByteQRCode qRCode = new PngByteQRCode(data);
    byte[] image = qRCode.GetGraphic(10,
                    darkColorRgba: new byte[] { 0, 90, 0 }, // RGB
                    lightColorRgba: new byte[] { 255, 255, 255 }); // RGB
    
  2. Use the helper methods provided by the PngByteQRCodeHelper static class to generate images instead.

    The main difference between this and the .NET Framework usage is that the GetQRCode() method of the PngByteQRCodeHelper class returns a byte[] instead of a Bitmap.

    var url = "https://blog.miniasp.com";
    byte[] image = PngByteQRCodeHelper.GetQRCode(url, QRCodeGenerator.ECCLevel.Q, 10);
    

What is ECC Level?

ECC Level is an abbreviation for Error Correction Code Level. The higher the level, the more errors are allowed!

The ECC levels represent the degree to which a QR Code reader can tolerate a damaged or distorted QR Code image, and are categorized into four levels, from L (Lowest) to H (Highest):

  1. ECC Level L: This level can tolerate up to 7% of a QR Code image being damaged or distorted (L = Lowest).
  2. ECC Level M: This level tolerates QR Code images with up to 15% damage or distortion ( M = Medium )
  3. ECC Level Q: This level can tolerate QR Code images with up to 25% damage or distortion ( Q = Quarter )
  4. ECC Level H: This level tolerates QR Code images with up to 30% damage or distortion ( H = Highest )

Generally speaking, most people choose the Q level of error tolerance because it allows 25% (1/4) of errors and the image size is not too large.

Generate a QRCode image in text format

If you want to output a QRCode image in Console mode, you can use the GetQRCode() helper method provided by the AsciiQRCodeHelper static class to generate a text-formatted image, which is similar to the PngByteQRCodeHelper, except that the return type is string.

string txt = AsciiQRCodeHelper.GetQRCode(url,
    pixelsPerModule: 1,
    darkColorString: "██",
    whiteSpaceString: "  ",
    eccLevel: QRCodeGenerator.ECCLevel.Q);
Console.WriteLine(txt);

Here, the darkColorString parameter should be put into two U+2588 characters (full block), while the whiteSpaceString parameter should be put into two blank characters, so be careful to use equal-width fonts when outputting.


Reference: https://blog.miniasp.com/post/2023/08/30/How-to-use-QRCoder-generates-QR-Code-using-dotNet