Monday 4 November 2013

How to create Dynamic QR Code using ASP.NET



In this article I will explain how to create Dynamic QR Code using ASP.NET

Definition:

It is a machine-readable code consisting of an array of black and white squares, typically used for storing URLs or other information for reading by the camera on a smartphone.

Below Datalist designing code is useful for binding QR Code images.

<asp:DataList Width="100%" CellSpacing="0" ItemStyle-Width="190px" runat="server"
 ID="dlLevel2" RepeatColumns="7" DataKeyField="ID" GridLines="None" RepeatDirection="Horizontal">
               <ItemTemplate>
                  <center>
                    <div>
                      <asp:Label ID="Image1" Text='<%#bind("ID") %>' runat="server" />
                    </div>
                    <div style="height: 100px; vertical-align: text-top; width: 120px">
                   <asp:Image ID="img1" ImageUrl=' <%# DataBinder.Eval(Container.DataItem, "QRImage") %>'
                                                runat="server" Width="80px" />
                     </div>
                 </center>
              </ItemTemplate>
</asp:DataList>



DataList image is shown below.


C#: 

Need to add below Namespaces for Creating Dynamic QR Code images.

using System.Drawing;
using System.Drawing.Imaging;
using MessagingToolkit.QRCode.Codec;
using MessagingToolkit.QRCode.Codec.Data;
using System.IO;
using System.Text;

Add Below class for Encode actual data .

QRCodeEncoder encoder = new QRCodeEncoder();
        Bitmap img = null;

Below is the button click event code. 

  protected void btnGenerateQR_Click(object sender, EventArgs e)
        {
            GenerateQr();
        }

This method is used for Getting data from DataBase and bind data to Dataset. Using dataset information to we will create Dynamic QR Code images. Bind Dynamic QR Code images to DataList.
public void GenerateQr()
        {
            try
            {
                ds = objClass.GetData();
                string[] filePaths = Directory.GetFiles(Server.MapPath("~/CreateFolder/"));
                foreach (string filePath in filePaths)
                    File.Delete(filePath);
              
                if (dt.Columns.Count == 0)
                {
                    dt.Columns.Add("ID");
                    dt.Columns.Add("QRImage");
                }

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                  
                    string fileName = ds.Tables[0].Rows[i][0].ToString().Replace(' ', '_') + ".jpg";
                    string path = Server.MapPath("~/CreateFolder/") + fileName;
                
                    Bitmap img = encoder.Encode((ds.Tables[0].Rows[i][8].ToString().Trim()) + "," + (ds.Tables[0].Rows[i][1].ToString().Trim()) + "," + (ds.Tables[0].Rows[i][2].ToString().Trim()) + "," +(ds.Tables[0].Rows[i][3].ToString().Trim()) + "," + (ds.Tables[0].Rows[i][4].ToString().Trim()));
                    img.Save(path, ImageFormat.Jpeg);

                    DataRow drSubject = dt.NewRow();
                  
                    drSubject["QRImage"] = "~/CreateFolder/" + fileName;
                    drSubject["ID"] = ds.Tables[0].Rows[i][8].ToString();
                 

                    dt.Rows.Add(drSubject);
                                }

                if (dt.Rows.Count > 0)
                {
                    dlLevel2.DataSource = dt;
                    dlLevel2.DataBind();
                }
                btnPrintAllCodes.Visible = true;
            }
            catch (Exception ex)
            {

            }
        }




No comments:

Post a Comment