Search code examples
c#reactjsqr-codezxing

Create QRCode with C# and save it as an image


I'm receiving data from a URL on our web application that's dynamically generated using an API's response. I have to handle it by putting it in a QRCode and send this QRCode back to the front-end for my client to download it. We're using C# for back-end and React.js for the front-end.

I've tried to use ZXIng libraries for C# but with no success. I've tried a lot of code, but nothing seems to happen.


Solution

  • Try the following code. It's been tested, but you may need to adapt it for your usage which shouldn't be too difficult.

    Download/install NuGet package: ZXing.Net (version: 0.16.8)

    Add the following using directives:

    • using System.IO;
    • using ZXing;
    • using ZXing.Common;
    • using ZXing.QrCode;

    Code:

    private Bitmap CreateQrCode(string data)
    {
        //specify desired options
        QrCodeEncodingOptions options = new QrCodeEncodingOptions()
        {
            CharacterSet = "UTF-8",
            DisableECI = true,
            Width = 250,
            Height = 250
        };
    
        //create new instance and set properties
        BarcodeWriter writer = new BarcodeWriter()
        {
            Format = BarcodeFormat.QR_CODE,
            Options = options
        };
    
        //create QR code and return Bitmap
        return writer.Write(data);
    }
    
    private byte[] GetQrCodeBytes(string data, System.Drawing.Imaging.ImageFormat imgFormat)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            //create QR code and save to file
            using (Bitmap bmp = CreateQrCode(data))
            {
                //save to MemoryStream
                bmp.Save(ms, imgFormat);
            }
    
            return ms.ToArray();
        }
    }
    
    private string GetTextFromQrCode(byte[] qrcodeBytes)
    {
        //specify desired options
        DecodingOptions options = new DecodingOptions()
        {
            CharacterSet = "UTF-8"
        };
    
        //create new instance and set properties
        BarcodeReader reader = new BarcodeReader() { Options = options };
    
        using (MemoryStream ms = new MemoryStream(qrcodeBytes))
        {
            using (Bitmap bmp = (Bitmap)Bitmap.FromStream(ms))
            {
                //decode QR code
                Result r = reader.Decode(bmp);
    
                //return QR code text
                return r.Text;
            }
        }
    }
    
    private string GetTextFromQrCode(string filename)
    {
        //specify desired options
        DecodingOptions options = new DecodingOptions()
        {
            CharacterSet = "UTF-8"
        };
    
        //create new instance and set properties
        BarcodeReader reader = new BarcodeReader() { Options = options };
    
        //read image and convert to Bitmap
        using (Bitmap bmp = (Bitmap)Bitmap.FromFile(filename))
        {
            //decode QR code
            Result r = reader.Decode(bmp);
    
            //return QR code text
            return r.Text;
        }
    }
    
    private void SaveQrCode(string data, string filename, System.Drawing.Imaging.ImageFormat imgFormat)
    {
        //create QR code and save to file
        using (Bitmap bmp = CreateQrCode(data))
        {
            bmp.Save(filename, imgFormat);
        }
    }
    

    Decode QR Code (Usage 1):

    string filename = @"C:\Temp\TestQrCode.png"
    string qrcodeText = GetTextFromQrCode(filename);
    

    Decode QR Code (Usage 2):

    Note: In the code below, the QR code is read from a file and placed in a byte[]. Reading from a file is for demonstration/testing purposes.

    string filename = @"C:\Temp\TestQrCode.png"
    byte[] qrcodeBytes = File.ReadAllBytes(filename);
    string qrcodeText = GetTextFromQrCode(qrcodeBytes);
    

    Save QR Code (Usage 1):

    string qrcodeText = "This is a test";
    string filename = @"C:\Temp\TestQrCode.png"
    
    //get filename extension
    string ext = Path.GetExtension(filename);
    
    if (ext == ".bmp" || ext == ".dib" || ext == ".rle")
        SaveQrCode(qrcodeText, filename, System.Drawing.Imaging.ImageFormat.Bmp);
    else if (ext == ".jpg" || ext == ".jpeg" || ext == ".jfif" || ext == ".jpe")
        SaveQrCode(qrcodeText, fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
    else if (ext == ".png")
        SaveQrCode(qrcodeText, fileName, System.Drawing.Imaging.ImageFormat.Png);
    

    Save QR Code (Usage 2):

    Note: In the code below, the QR code is saved to a byte[], then written to a file. Writing to a file is for demonstration/testing purposes.

    string qrcodeText = "This is a test";
    string filename = @"C:\Temp\TestQrCode.png"
    
    //get filename extension
    string ext = Path.GetExtension(filename);
    
    byte[] qrcodeBytes = null;
    
    if (ext == ".bmp" || ext == ".dib" || ext == ".rle")
        qrcodeBytes = GetQrCodeBytes(qrcodeText, System.Drawing.Imaging.ImageFormat.Bmp);
    else if (ext == ".jpg" || ext == ".jpeg" || ext == ".jfif" || ext == ".jpe")
        qrcodeBytes = GetQrCodeBytes(qrcodeText, System.Drawing.Imaging.ImageFormat.Jpeg);
    else if (ext == ".png")
        qrcodeBytes = GetQrCodeBytes(qrcodeText, System.Drawing.Imaging.ImageFormat.Png);
    
    //save to file
    File.WriteAllBytes(fileName, qrcodeBytes);
    

    Resources