generate qrcode store image name in database encrypted sha1 formate using c#

using System;
using System.Web;
using QRCoder;
using System.IO;
using System.Drawing;
using System.Text;
using System.Data.SqlClient;
using System.Security.Cryptography;

public partial class CS : System.Web.UI.Page
{
    //Connestion String In Database
    SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\qrdb.mdf;Integrated Security=True"); 
    protected void btnGenerate_Click(object sender, EventArgs e)
    {
        //Get TextBox Value
        string code = txtCode.Text;
        //Create QRCodeGenerator class object
        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();
        //Set Height And Width
        imgBarCode.Height = 150;
        imgBarCode.Width = 150;
        using (Bitmap bitMap = qrCode.GetGraphic(20))
        {
            using (MemoryStream ms = new MemoryStream())
            {
                bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                byte[] byteImage = ms.ToArray();
                string name = code+ ".PNG";
                using (SHA1 sha1Hash = SHA1.Create())
                {
                    byte[] sourceBytes = Encoding.UTF8.GetBytes(name);
                    byte[] hashBytes = sha1Hash.ComputeHash(sourceBytes);
                    string hash = BitConverter.ToString(hashBytes).Replace("-", String.Empty);
                    //Get Value With JPG Extension
                    string hashvalue = hash + ".PNG";
                    System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
                    img.Save(Server.MapPath("Images/") + hashvalue);
                    imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
                    string result = Convert.ToBase64String(byteImage, 0, byteImage.Length);
                    //Store Database Image Path In Hase Value
                    con.Open();
                    SqlCommand cmd = new SqlCommand("insert into qr_table values('" + hashvalue + "')", con);
                    cmd.ExecuteNonQuery();
                    con.Close();
                }                           
            }
            plBarCode.Controls.Add(imgBarCode);
        }
    }
}

Post a Comment

0 Comments