String to base64 QR code image

Install following third-party plugin


PM> Install-Package QRCoder

 Create a new class called "QRCodeImageGenerator" and copy past the following code


// namespaces
namespace Client_DAL.Helper
{
    using QRCoder;
    using System;
    public static class QRCodeImageGenerator
    {
        public static string GetBase64QRCode(string imageText, out string errMsg)
        {
            var result = string.Empty;
            errMsg = string.Empty;
            try
            {
                QRCodeGenerator qrGenerator = new();
                QRCodeData qrCodeData = qrGenerator.CreateQrCode(imageText, QRCodeGenerator.ECCLevel.Q);
PngByteQRCode qrCode = new(qrCodeData); byte[] qrCodeImage = qrCode.GetGraphic(20);
result = $"data:image/png;base64,{Convert.ToBase64String(qrCodeImage)}"; } catch (Exception ex) { errMsg = "Unable to generate QRCode"; } return result; } } }

call method like below


var qrCode = QRCodeImageGenerator.GetBase64QRCode(sometext, out string _);

 


Summary:

Convert string to QRCode Base64 image