Tuesday, January 14, 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 library to our application. below image shows how to install Zxing.net package.
click on
             Tools --> Nugut Package manager
                       --> manage nugut 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 purpose, i just created the one Text Box and one button.
Text Box is used for input values
Button is used for generated QR Code as show 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 out put shows as below image

zxing qr code generator in c sharp




No comments: