Display QR Code Generated Name,Link in MVC C#


   I Already mentioned How to Create QR Code Generation in MVC .


Controller Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Gma.QrCodeNet.Encoding;
using Gma.QrCodeNet.Encoding.Windows.Render;
using System.Data;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace QRPrint.Controllers
{
    public class QRControlController : Controller
    {
        string filepath = HttpRuntime.AppDomainAppPath;

        [HttpGet]
        public ActionResult Display(String txt="")
        {
           // try
            //{
                if(txt!="")
                {
                   QRPrintCodeImage(txt);
                    ViewBag.txt = txt + ".png";
                }
               return View();
            //}
            //catch (Exception ex)
            //{
              //  return View();
            //}

        }
        [HttpPost]
        public ActionResult Display(FormCollection Data)
        {
            string name = Convert.ToString(Data["txtname"]);
            return RedirectToAction("Display",new { txt=name });
        }

        public ActionResult QRPrintCodeImage(String Text)
        {
           
            // generating a barcode here. Code is taken from QrCode.Net library
            QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
            QrCode qrCode = new QrCode();
            qrEncoder.TryEncode(Text, out qrCode);

            System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
            imgBarCode.Height = 140;
            imgBarCode.Width = 140;

            GraphicsRenderer renderer = new GraphicsRenderer(new FixedModuleSize(4, QuietZoneModules.Four), Brushes.Black, Brushes.White);

            MemoryStream memStream = new MemoryStream();
            renderer.WriteToStream(qrCode.Matrix, ImageFormat.Png, memStream);
            // very important to reset memory stream to a starting position, otherwise you would get 0 bytes returned
            memStream.Position = 0;

            var resultStream = new FileStreamResult(memStream, "image/png");
            resultStream.FileDownloadName = String.Format("{0}.png", Text);

            // Save To Drive
            byte[] byteImage = memStream.ToArray();

            if (!Directory.Exists(filepath))
                Directory.CreateDirectory(filepath);

            string fileLocation = filepath + Text + ".png";

            using (MemoryStream msm = new MemoryStream(byteImage))
            {
                using (Bitmap bmp2 = new Bitmap(msm))
                {
                    if (System.IO.File.Exists(fileLocation))
                    {
                        System.IO.File.Delete(fileLocation);
                    }
                   
                    bmp2.Save(filepath + "\\Images\\" + Text + ".png");
                }
            }
            return resultStream;
        }
    }

}

View Code:

      
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Display</title>
    <script>
        $(document).ready(function () {
            var name = '@Request.QueryString["txt"]';
        });
    </script>


</head>
<body>
    <div>
        <form method="post">
            Name: <input type="text" id="txtname" name="txtname" />
            <input type="submit" value="QR Gen" />
            <img src="~/Images/@ViewBag.txt" height="220" width="220"/>
        </form>
    </div>
</body>
</html>

Result

and use any QR Code or Barcode Reader in Your Mobile.






Comments

Popular posts from this blog

Insecure cookie setting: missing Secure flag

Maximum Stored Procedure Function Trigger or View Nesting Level Exceeded (limit 32) in SQL Server

Display Line Chart Using Chart.js MVC C#