Sunday, April 26, 2020

QR Code Generator in Asp.net C# using ZXing.Net Library

c# qr code reader

Generating the bar code is very easy, we have so many libraries are there. out of that, I am going to choose zxing QR, this assembly is very good for generating the QR code and reading the QR code. by using asp.net, we can generate and read the QR Code is very easy in c#.

Create asp.net web application then add zxing QR library to our application. The below image shows how to install the Zxing.net package.
click on
             Tools --> Nugut Package manager
                       --> manage NuGet packages for solutions
                       --> go to browse search for Zxing.net.
                       --> install it.
And check by clicking References, we can find the zing assembly. once it is available then start our sample project


zxing qr code generator c#


for understanding purposes, I just created the one Text Box and one button.
Text Box is used for input values
The button is used for generated QR Code as shown below image.

zxing qr code generator asp.net

Now we need to write the code "zxing QR code generator c#"

 using System;
using ZXing;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;


namespace BarCode
{
    public partial class alltechgeeksBARCODE : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnGetBarCode_Click(object sender, EventArgs e)
        {
            string name = txtToGenerateCode.Text;
            var writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            var result = writer.Write(name);
            string path = Server.MapPath("~/Barcodeimages/QRImage.jpg");
            var barcodeBitmap = new Bitmap(result);
            using (MemoryStream memory = new MemoryStream())
            {
                using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
                {
                    barcodeBitmap.Save(memory, ImageFormat.Jpeg);
                    byte[] bytes = memory.ToArray();
                    fs.Write(bytes, 0, bytes.Length);
                }
            }
            imgQROrBarCode.Visible = true;
            imgQROrBarCode.ImageUrl = "~/Barcodeimages/QRImage.jpg";
        }
    }
}

The output shows as below image

zxing qr code generator in c sharp




No comments: