manuelbl / QrCodeGenerator

QR Code Generator for .NET
MIT License
306 stars 77 forks source link

Bug with converting to base64 #4

Closed ramax495 closed 3 years ago

ramax495 commented 3 years ago

Hi!

I need to show result qr-code in img html tag using base64 encoding. e.g.

When I use options Ecc.High and Ecc.Quartile all is ok.

But when I use options Ecc.Low and Ecc.Medium image is not shown.

I explored the result base64 string and saw that it had incorrect format. At the end of the string there is only one symbol "=" (but must be "=="). And If I take away several last symbols (about 600) of the string and add "=" symbol, everything is getting ok.

Also when I save bitmap to file (.png), everything is good. Problem is only with base64 string.

May be some unnesassary bytes get to bitmap when using options Low and Medium ?

My code:

public string ToBase64(string data)
{
            var qrCode = QrCode.EncodeText(data, QrCode.Ecc.Low);
            using (var ms = new MemoryStream())
            using (var bitmap = qrCode.ToBitmap(6, 10);)
            {
                bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                return Convert.ToBase64String(ms.GetBuffer());
            }
}
manuelbl commented 3 years ago

The Base 64 encoded string is generated by Convert.ToBase64String(). This is a function of the .NET framework and not related to the QR generator code.

So if you think the function generates invalid Base 64, you should contact Microsoft or a .NET related forum. However, I somewhat doubt this function has a bug that hasn't been discovered in the last few years.