Create a QR Code with a Logo in ASP.Net C#

Category > ASP.NET || Published on : Wednesday, January 27, 2016 || Views: 11389 || Create a QR Code with a Logo in ASP.Net C# Create a QR Code


Introduction

Here Pawan Kumar will explain how to Create a QR Code with a Logo in ASP.Net C#

Description

In previous post I have explained Cascading dropdown in ASP.NET using JQuery Http Handler, How to add current copyright year in ASP.NET, How to avoid a form from double submission using asp.net, Set max length of MultiLine TextBox in ASP.Net, Allow only alphabets in a textbox using javascript in asp.net, jQuery, ASP.NET - How to validate the file type and file size of File Upload control on file selection using the jQuery .Change() event, and many more articles.

Now I will explain How to Create a QR Code with a Logo in ASP.Net C#

So follow the steps to learn Create a QR Code with a Logo in ASP.Net C#
ASP.NET Page
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="URL" runat="server"></asp:TextBox>
        <br /><br />
        <asp:FileUpload ID="LogoUpload" runat="server" />
        <br /><br />
        <asp:Button ID="CreateCode" runat="server" Text="Create QR Code" OnClick="CreateCode_OnClick" />
        <br /><br />
        <asp:Image runat="server" ID="QRImage" />
    </div>
    </form>
</body>
</html>

ASP.NET Code Behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MessagingToolkit.QRCode.Codec;
using MessagingToolkit.QRCode.Codec.Data;
using System.Drawing;
using System.Drawing.Imaging; 

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

        protected void CreateCode_OnClick(object sender, EventArgs e)
        {
            string path = "c:\\code\\projects\\CodeCreator\\CodeCreator\\";
            QRCodeEncoder encoder = new QRCodeEncoder();
            encoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H; // 30%
            encoder.QRCodeScale = 10;

            Bitmap img = encoder.Encode(URL.Text);
            LogoUpload.SaveAs(path + LogoUpload.FileName);
            System.Drawing.Image logo = System.Drawing.Image.FromFile(path + LogoUpload.FileName);

            int left = (img.Width / 2) - (logo.Width / 2);
            int top = (img.Height / 2) - (logo.Height / 2); 

            Graphics g = Graphics.FromImage(img);
            g.DrawImage(logo, new Point(left, top));            

            img.Save(path + "img.jpg", ImageFormat.Jpeg); 

            QRImage.ImageUrl = "img.jpg";
        }
    }
}

 

Conclusion:

So, In this tutorial we have learned, Create a QR Code with a Logo in ASP.Net C#