Tuesday, 9 January 2018

Dynamically generate and display QR Code Image in ASP.Net MVC

Dynamically generate and display QR Code Image in ASP.Net MVC
Description:

In this example we explain that how to dynamically generate the QR code Image in Asp.Net MVC.or how to create Dynamically QR code in MVC using Razor View. Or how to dynamically generate QR code and display it in Razor View in MVC.

Here we use the QRCoder dll to generate or display the QR code in MVC Razor view.so how to create QR code in MVC Razor.
Controller:

public class EmployeeController : Controller
    {

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(string qrcode)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                QRCodeGenerator qrGenerator = new QRCodeGenerator();
                QRCodeGenerator.QRCode qrCode = qrGenerator.CreateQrCode(qrcode, QRCodeGenerator.ECCLevel.Q);
                using (Bitmap bitMap = qrCode.GetGraphic(20))
                {
                    bitMap.Save(ms, ImageFormat.Png);
                    ViewBag.QRCodeImage = "data:image/png;base64," + Convert.ToBase64String(ms.ToArray());
                }
            }

            return View();
        }
    }

View: 

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <title>Dynamically generate and display QR Code Image in ASP.Net MVC </title>
</head>
<body>
    @using (Html.BeginForm("Index", "Employee", FormMethod.Post))
    {
        <input type="text" name="qrcode"/>
        <input type="submit" value="Generate"/>
    }
    <hr/>
    @if (ViewBag.QRCodeImage != null)
    {
        <img src="@ViewBag.QRCodeImage" alt="" style="height:150px;width:150px"/>
    }
</body>
</html>


This entry was posted in :

1 comments: