Introduction
In this article we will learn about how to generate QR code from a input text and read the QR code on button click. Generate QR code and Read QR code using Asp.Net C#.Previous Updates
In previous articles we have learnt Configure Setup(Installer) project in VS2015. What is CTE in SQL and use . If Else and Case statement , While Loop . What is Cursor in sql and use of cursor. Difference between Row_Number(), Rank(), Rank_Density() with example.
Practice
We can generate QR code and also read data from generated QR code image using Zxing.Net in Asp.net application. You can add Zxing.Net library directly from package manage console.Steps to Create QR code and Read QR code image
1. Add a Asp.Net Web Application and gave a suitable name to it.
2. Add a new Web Form on it and gave a name to it.
3. Click on Visual Studio's Tools Menu, click on NuGet Package Manager and select Manage NugGet Package For Solution
4. Search for Zxing.Net and then click on Install and wait for installation process to be finish.
5. After successfully installation of Zxing.Net, go to the added Web form on step 2.
6. Just copy the below given aspx code and paster on your Web-Form.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtCode" runat="server"></asp:TextBox>
<asp:Button ID="btnGenerateQR" runat="server" Text="Generate QR Code"
OnClick="btnGenerateQR_Click" />
<hr />
<asp:Image ID="imgQR" Width="100px" Height="100px" runat="server"
Visible="false" />
<br />
<br />
<asp:Button ID="btnReadQR" Text="Read QR Image" runat="server"
OnClick="btnReadQR_Click" Visible="false"/>
<br />
<br />
<asp:Label ID="lblQRCode" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
|
7. Go to Code View on aspx.cs page and paste the all code given below.
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnGenerateQR_Click(object sender, EventArgs e)
{
GenerateQRCode(txtCode.Text);
btnReadQR.Visible = true;
txtCode.Text = string.Empty;
}
protected void btnReadQR_Click(object sender, EventArgs e)
{
ReadQRCode();
}
// Generate QRCode for given text
private void GenerateQRCode(string name)
{
var writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
var result = writer.Write(name);
string path = Server.MapPath("~/Images/QR.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);
fs.Close();
}
}
imgQR.Visible = true;
imgQR.ImageUrl = "~/Images/QR.jpg";
}
// Read the QR Code from QR Image
private void ReadQRCode()
{
var reader = new BarcodeReader();
string filename = Path.Combine(Request.MapPath("~/Images"),
"QR.jpg");
// Detect and decode the barcode inside the bitmap
var result = reader.Decode(new Bitmap(filename));
if (result != null)
{
lblQRCode.Text = "QR Code: " + result.Text;
}
}
|
8. Now run the application and your Web form look like this
9. Enter any text and click on Generate QR code button.
10. Click on Read QR Code , the text from that QR image will be displayed.
I am thankful to this blog giving unique and helpful knowledge about this topic.
ReplyDeleteQr Code Generation Process