ZXing QrCode renderer exception with .Net Core 2.1
Asked Answered
A

4

9

I would like to create a QR code with using ZXing(0.16.4) But I meet following exception,

System.InvalidOperationException: 'You have to set a renderer instance.'

Almost the same code works well with .Net Framework 4.6.1

here is my code

static void Main(string[] args)
{
    var qrCode = CreateQrCode("test");
    Console.ReadKey();
}

public static byte[] CreateQrCode(string content)
{
    BarcodeWriter<Bitmap> writer = new BarcodeWriter<Bitmap>
    {
        Format = BarcodeFormat.QR_CODE,
        Options = new QrCodeEncodingOptions
        {
            Width = 100,
            Height = 100,
        }
    };

    var qrCodeImage = writer.Write(content); // BOOM!!

    using (var stream = new MemoryStream())
    {
        qrCodeImage.Save(stream, ImageFormat.Png);
        return stream.ToArray();
    }
}
Analog answered 3/10, 2018 at 6:17 Comment(0)
A
7

I solved the issue, Basically I used https://www.nuget.org/packages/ZXing.Net.Bindings.CoreCompat.System.Drawing

I create BarcodeWriter generated from following namespace

ZXing.CoreCompat.System.Drawing

here is my CreateQrCode method

public static byte[] CreateQrCode(string content)
{
    BarcodeWriter writer = new BarcodeWriter
    {
        Format = BarcodeFormat.QR_CODE,
        Options = new QrCodeEncodingOptions
        {
            Width = 100,
            Height = 100,
        }
    };

    var qrCodeImage = writer.Write(content); // BOOM!!

    using (var stream = new MemoryStream())
    {
        qrCodeImage.Save(stream, ImageFormat.Png);
        return stream.ToArray();
    }
}

Here is the read QR code method, maybe someone will need as well. BarcodeReader also generated from the same namespace like create.

Here is the method

public static string ReadQrCode(byte[] qrCode)
{
    BarcodeReader coreCompatReader = new BarcodeReader();

    using (Stream stream = new MemoryStream(qrCode))
    {
        using (var coreCompatImage = (Bitmap)Image.FromStream(stream))
        {
            return coreCompatReader.Decode(coreCompatImage).Text;
        }
    }
}

Hope this answer will protect someone's hair against pulling.

Analog answered 3/10, 2018 at 7:18 Comment(0)
F
4

I'm currently moving to .net 6 and I used BarcodeWriter from ZXing.Net.Bindings.SkiaSharp NuGet package.

using ZXing.SkiaSharp;
var barcodeWriter = new BarcodeWriter
{
     Format = BarcodeFormat.QR_CODE,
     Options = new EncodingOptions
     {
         Height = _height,
         Width = _width,
         Margin = _margin
     }
};
using var bitmap = barcodeWriter.Write(qrValue);
using var stream = new MemoryStream();
bitmap.Encode(stream, SKEncodedImageFormat.Png, 100);

Your stream is filled now :)

Flabellum answered 12/10, 2022 at 9:8 Comment(0)
L
3

There is a newer version of the package available and it works with .NET Core 3.1.

https://www.nuget.org/packages/ZXing.Net.Bindings.Windows.Compatibility/

Lifegiving answered 1/5, 2020 at 20:29 Comment(1)
Works perfectly with .NET CoreGalang
B
2

I needed to add "Renderer = new ZXing.Rendering.BitmapRenderer()" when using ZXing.Net v0.16.6

public static byte[] CreateQrCode(string content)
{
    byte[] imageData;

    var qrWriter = new ZXing.BarcodeWriter<System.Drawing.Bitmap>
    {
        Format = BarcodeFormat.QR_CODE,
        Options = new ZXing.Common.EncodingOptions { Height = 100, Width = 100, Margin = 0 },
        Renderer = new ZXing.Rendering.BitmapRenderer()
    };

    using (var ms = new System.IO.MemoryStream())
    using (System.Drawing.Bitmap pixelData = qrWriter.Write(content))
    {
        pixelData.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        imageData = ms.ToArray();
    }

    return imageData;
}
Booted answered 3/11, 2021 at 21:15 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.