Dynamically generate and display QR code image in ASP.Net c#

QR code (abbreviated from Quick Response Code) is the trademark for a type of matrix barcode (or two-dimensional barcode) .Now a days we have every book and packet in QR code that attached with website .So in this article , we will learn about that how to dynamically generate and show QR code image in asp.net c#


Source Code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title> QR code image in ASP.Net c#</title>
</head>
<body>
    <form id="form1" runat="server">
    
    <asp:TextBox ID="txtqccode" runat="server"></asp:TextBox>
    <asp:Button ID="btngenerate" runat="server" Text="Generate" OnClick="btngenerate_Click" />
    <hr />
    <asp:PlaceHolder ID="plBarCode" runat="server" />
  
    </form>
</body>
</html>

Code Behind(c#):-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QRCoder;
using System.IO;
using System.Drawing;

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

    protected void btngenerate_Click(object sender, EventArgs e)
    {
        string code = txtqccode.Text;
        QRCodeGenerator qcgenerator = new QRCodeGenerator();
        QRCodeGenerator.QRCode qrcode = qcgenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q);
        System.Web.UI.WebControls.Image imgbarcode = new System.Web.UI.WebControls.Image();
        imgbarcode.Height = 200;
        imgbarcode.Width = 200;
        using (Bitmap bitmap = qrcode.GetGraphic(20))
        {
            using (MemoryStream ms = new MemoryStream())
            {
                bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                byte[] byteImage = ms.ToArray();
                imgbarcode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
            }
            plBarCode.Controls.Add(imgbarcode);
        }
    }
}

Note: - You will need to download the QR code dll;

Out-Put:-








Dynamically generate and display QR code image in ASP.Net c# Dynamically generate and display QR code image in ASP.Net c# Reviewed by NEERAJ SRIVASTAVA on 10:39:00 AM Rating: 5

No comments:

Powered by Blogger.