How to generate QR codes with ASP.NET MVC?

In this post we will see how to generate QR codes with ASP.NET MVC.


Step 1.

First create a new MVC project as shown in the following images:

generar códigos QR con ASP.NET MVC

QR con ASP.NET

Step 2.

Add the QRCoder library you find it in the NuGet packages

Libreria gratis para generar QR con ASP.NET

Step 3

Create a new controller and its corresponding view, then you must copy and paste the following html.


@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>QR Generate</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</head>
<body>
    <form id="form1" runat="server" action="~/Default" method="post">
        <div class="container">
            <h2>How to Generate QR Code in ASP.NET MVC</h2>
            <div class="row">
                <div class="col-md-9">
                    <div class="form-group">
                        <label>Enter Something</label>
                        <div class="input-group">
                            <input type="text" name="txtQRCode" class="form-control" value="@ViewBag.txtQRCode" />
                            <div class="input-group-prepend">
                                <button type="submit">Generte QR</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            @if (ViewBag.imageBytes != null)
            {
                <img src="@String.Format("data:image/png;base64,{0}", Convert.ToBase64String(ViewBag.imageBytes))" />
            }

        </div>
    </form>
</body>
</html>

Step 4.

Copy and paste the server code and go ahead run the example.

using QRCoder;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace QR.Controllers
{
    public class DefaultController : Controller
    {
        // GET: Default
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(string txtQRCode)
        {
            ViewBag.txtQRCode = txtQRCode;
            QRCodeGenerator qrGenerator = new QRCodeGenerator();
            QRCodeData qrCodeData = qrGenerator.CreateQrCode(txtQRCode, QRCodeGenerator.ECCLevel.Q);
            QRCode qrCode = new QRCode(qrCodeData);
            //System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
            //imgBarCode.Height = 150;
            //imgBarCode.Width = 150;
            using (Bitmap bitMap = qrCode.GetGraphic(20))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    ViewBag.imageBytes = ms.ToArray();
                    //imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
                }
            }
            return View();
        }
    }
}

 

Artículos relacionados
0 Comentarios
Enviame un comentario