Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Create or Generate QR Code in Asp.Net using C#, VB.NET

Apr 16, 2017
Introduction:

Here we will learn how to create or generate QR code in asp.net web application using c#, vb.net with example or asp.net dynamically generate and display QR code using c#, vb.net with example or generate and read QR code in asp.net using zxing.Net library in c#, vb.net with example. By using “Zxing.Net” library in asp.net we can easily generate and read QR code in c#, vb.net with example based on our requirements.

Description:


In asp.net by using “Zxing.Net” library we can generate QR code and read data from that image based on our requirements.

To use “Zxing.Net” library in our application first create new asp.net web application and add Zxing.Net library for that right click on your application à select Manage Nuget Packages à Go to Browse Tab à Search for Zxing.Net à From the list select ZxingNet and install it. Once we install the component that will show like as shown following.

Install zxing.net package to create QR code in asp.net web application
Once we install Zxing.Net package in our application now open your aspx page and write the code like as shown following.


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Generate and Read QR Code in Asp.Net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtCode" runat="server"></asp:TextBox>
<asp:Button ID="btnGenerate" runat="server" Text="Generate QR Code" OnClick="btnGenerate_Click" />
<hr />
<asp:Image ID="imgQRCode" Width="100px" Height="100px" runat="server" Visible="false" />     <br /><br />
<asp:Button ID="btnRead" Text="Read QR Image" runat="server" OnClick="btnRead_Click" />  <br /><br />
<asp:Label ID="lblQRCode" runat="server"></asp:Label>
</div>
</form>
</body>
</html>

Now open code behind file and write the code like as shown following

C# Code


using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using ZXing;

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

}
protected void btnGenerate_Click(object sender, EventArgs e)
{
GenerateCode(txtCode.Text);
}
protected void btnRead_Click(object sender, EventArgs e)
{
ReadQRCode();
}
// Generate QRCode
private void GenerateCode(string name)
{
var writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
var result = writer.Write(name);
string path = Server.MapPath("~/images/QRImage.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);
}
}
imgQRCode.Visible = true;
imgQRCode.ImageUrl = "~/images/QRImage.jpg";

}
// Read Code from QR Image
private void ReadQRCode()
{
var reader = new BarcodeReader();
string filename = Path.Combine(Request.MapPath("~/images"), "QRImage.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;
}
}
}

VB.NET Code


Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO
Imports ZXing
Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs)

End Sub
Protected Sub btnGenerate_Click(sender As Object, e As EventArgs)
GenerateCode(txtCode.Text)
End Sub
Protected Sub btnRead_Click(sender As Object, e As EventArgs)
ReadQRCode()
End Sub
' Generate QRCode
Private Sub GenerateCode(name As String)
Dim writer = New BarcodeWriter()
writer.Format = BarcodeFormat.QR_CODE
Dim result = writer.Write(name)
Dim path As String = Server.MapPath("~/images/QRImage.jpg")
Dim barcodeBitmap = New Bitmap(result)

Using memory As New MemoryStream()
Using fs As New FileStream(path, FileMode.Create, FileAccess.ReadWrite)
barcodeBitmap.Save(memory, ImageFormat.Jpeg)
Dim bytes As Byte() = memory.ToArray()
fs.Write(bytes, 0, bytes.Length)
End Using
End Using
imgQRCode.Visible = True
imgQRCode.ImageUrl = "~/images/QRImage.jpg"

End Sub
' Read Code from QR Image
Private Sub ReadQRCode()
Dim reader = New BarcodeReader()
Dim filename As String = Path.Combine(Request.MapPath("~/images"), "QRImage.jpg")
' Detect and decode the barcode inside the bitmap
Dim result = reader.Decode(New Bitmap(filename))
If result IsNot Nothing Then
lblQRCode.Text = "QR Code: " + result.Text
End If
End Sub
End Class

If you observe above code we added a namespace “Zxing” reference in our application to generate and read QR code in our web applications.

Demo

When we run above code we will get result like as shown below

Asp.Net Generate and Read QR Code in Web Application using C#, VB.NET

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

9 comments :

Unknown said...

Hello Suresh,

Is there any possibility to generate QR code using Image

For example

I have one image and i want to convert it into QR code

Please let me know if it is possible?

Thank you in advance.

rawat said...

sir mujhe c# basic pdf or vidio chahiye
aapka youtubu link sent kr dijiye mere email id p
mera email id - ranjeetkumar1430143@gmail.com
mob -8521390143

Daani said...

QR code is good, but is it possible to put photo of existing QR code and scan to get the information?

Vikas said...
This comment has been removed by the author.
Anonymous said...

bro i too want to know how to make comment box work in blogs.. can you pls help me

pooja garg said...

can we put pic of existing QR code and able to do scanning?

Debasish said...

Superb

Sathish Sirikonda said...

can you give me link to download Zxing dll

Unknown said...

can you give me link to download Zxing dll

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.