Save image file from qrCode or base64 in asp.net C#

I want to save qrCode as image file in application path so that I can call it on report which is created in Crystal Report.

How to save qrCode as image file or convert base64 to an image and save on physical location?

My code is below.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        TextBox6.Text = GenerateAndGetString();

        QRCodeGenerator qrGenerator = new QRCodeGenerator();
        QRCodeData qrCodeData = qrGenerator.CreateQrCode(TextBox6.Text, QRCodeGenerator.ECCLevel.Q);
        QRCode qrCode = new QRCode(qrCodeData);
        Bitmap qrCodeImage = qrCode.GetGraphic(20);

        using (Bitmap bitMap = qrCode.GetGraphic(20))
        {
            using (MemoryStream ms = new MemoryStream())
            {
                bitMap.Save(ms, ImageFormat.Png);
                byte[] byteImage = ms.ToArray();
                img.Src = "data:image/png;base64," + Convert.ToBase64String(byteImage);
            }
        }
    }
}

public String GenerateAndGetString()
{
    var sellername = TextBox1.Text;
    var vatregistration = TextBox2.Text;
    var timestamp = TextBox3.Text;
    var invoiceamount = TextBox4.Text;
    var vatamoun = TextBox5.Text;

    return ConvertBase64(sellername, vatregistration, timestamp, invoiceamount, vatamoun);
}

>Solution :

You already got the image as byte-array, so you could do something like this:

System.IO.File.WriteAllBytes(path, ms.ToArray());

Leave a Reply