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 btnGeneratebr_Click(object sender, EventArgs e)
{
string barCode = txtCode.Text;
System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
using (Bitmap bitMap = new Bitmap(barCode.Length * 30, 60))
{
using (Graphics graphics = Graphics.FromImage(bitMap))
{
System.Drawing.Font oFont = new System.Drawing.Font("IDAutomationHC39M", 20);
PointF point = new PointF(2f, 2f);
SolidBrush blackBrush = new SolidBrush(Color.Black);
SolidBrush whiteBrush = new SolidBrush(Color.White);
graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
graphics.DrawString("*" + barCode + "*", oFont, blackBrush, point);
}
using (MemoryStream ms = new MemoryStream())
{
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] byteImage = ms.ToArray();
string result2 = Convert.ToBase64String(byteImage);
CreateImage(result2.ToString());
imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
}
plBarCode.Controls.Add(imgBarCode);
}
}
public string CreateImage(string Byt)
{
string barCode = txtCode.Text;
try
{
byte[] data = Convert.FromBase64String(Byt);
var filename = barCode + ".PNG";
using (var md5Hash = MD5.Create())
{
// Byte array representation of source string
var sourceBytes = Encoding.UTF8.GetBytes(filename);
// Generate hash value(Byte Array) for input data
var hashBytes = md5Hash.ComputeHash(sourceBytes);
// Convert hash byte array to string
var hash = BitConverter.ToString(hashBytes).Replace("-", string.Empty);
var hashvalue = hash + ".PNG";
// Output the MD5 hash
var file = HttpContext.Current.Server.MapPath("~/AppImagesBr/" + hashvalue);
System.IO.File.WriteAllBytes(file, data);
con.Open();
SqlCommand cmd = new SqlCommand("insert into qr_table values('" + hashvalue + "')", con);
cmd.ExecuteNonQuery();
con.Close();
return hashvalue;
}
}
catch (Exception e)
{
return e.Message;
}
}
}
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 btnGeneratebr_Click(object sender, EventArgs e)
{
string barCode = txtCode.Text;
System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
using (Bitmap bitMap = new Bitmap(barCode.Length * 30, 60))
{
using (Graphics graphics = Graphics.FromImage(bitMap))
{
System.Drawing.Font oFont = new System.Drawing.Font("IDAutomationHC39M", 20);
PointF point = new PointF(2f, 2f);
SolidBrush blackBrush = new SolidBrush(Color.Black);
SolidBrush whiteBrush = new SolidBrush(Color.White);
graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
graphics.DrawString("*" + barCode + "*", oFont, blackBrush, point);
}
using (MemoryStream ms = new MemoryStream())
{
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] byteImage = ms.ToArray();
string result2 = Convert.ToBase64String(byteImage);
CreateImage(result2.ToString());
imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
}
plBarCode.Controls.Add(imgBarCode);
}
}
public string CreateImage(string Byt)
{
string barCode = txtCode.Text;
try
{
byte[] data = Convert.FromBase64String(Byt);
var filename = barCode + ".PNG";
using (var md5Hash = MD5.Create())
{
// Byte array representation of source string
var sourceBytes = Encoding.UTF8.GetBytes(filename);
// Generate hash value(Byte Array) for input data
var hashBytes = md5Hash.ComputeHash(sourceBytes);
// Convert hash byte array to string
var hash = BitConverter.ToString(hashBytes).Replace("-", string.Empty);
var hashvalue = hash + ".PNG";
// Output the MD5 hash
var file = HttpContext.Current.Server.MapPath("~/AppImagesBr/" + hashvalue);
System.IO.File.WriteAllBytes(file, data);
con.Open();
SqlCommand cmd = new SqlCommand("insert into qr_table values('" + hashvalue + "')", con);
cmd.ExecuteNonQuery();
con.Close();
return hashvalue;
}
}
catch (Exception e)
{
return e.Message;
}
}
}
0 Comments
if you have any doubts , please let me know