Create and save QR code in asp.net with C#

Create and save QR code in asp.net with C#


Download Dll from given link

https://drive.google.com/drive/folders/0B2VobjpburSGdFBaZzBZSEJic0E?usp=sharing

.Aspx Page

<form id="form1" runat="server">
        <asp:TextBox ID="txtInput" runat="server"></asp:TextBox>
        <asp:Button ID="btnGenerate" runat="server" Text="Generate" OnClick="btnGenerate_Click" />
        <hr />
        <asp:PlaceHolder ID="plQRCode" runat="server" />
    </form>


.Cs Page


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 CS : System.Web.UI.Page
{
    protected void btnGenerate_Click(object sender, EventArgs e)
    {
        string code = txtInput.Text;
        QRCodeGenerator qrGenerator = new QRCodeGenerator();
        QRCodeGenerator.QRCode qrCode = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q);
        System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
        imgBarCode.Height = 250;
        imgBarCode.Width = 250;
        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);
                string result = Convert.ToBase64String(byteImage, 0, byteImage.Length); ;
                CreateImage(result.ToString());
            }
            plQRCode.Controls.Add(imgBarCode);
        }
    }
  
    public string CreateImage(string Byt)
    {
     
        try
        {
            byte[] data = Convert.FromBase64String(Byt);

            var filename = Convert.ToString(System.Guid.NewGuid()).Substring(0, 5) + Convert.ToString(System.Guid.NewGuid()).Substring(0, 5) + System.DateTime.Now.ToString("FFFFFF") + System.DateTime.Now.Minute + ".png";// +System.DateTime.Now.ToString("fffffffffff") + ".png";
            var file = HttpContext.Current.Server.MapPath("~/AppImages/" + filename);
            System.IO.File.WriteAllBytes(file, data);
            string ImgName = ".../profileimages/" + filename;

            return filename;
        }
        catch (Exception e)
        {
            return "Error";

        }
    }
}

It Will Look like :-


Comments

Post a Comment

Popular posts from this blog

Change text of RadGrid Header Dynamically

Telerik Radwindow Open on Button Click