MVC

How to generate QR codes with ASP.NET MVC?

Estrada Web Group
Administrador
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();
        }
    }
}

 

Compartir artículo:

Más artículos geniales

How to read an RSS and display the information in ASP.NET MVC?

How to read an RSS and display the information in ASP.NET MVC?

RSS means Rich Site Summary or also known as Really Simple Syndication. It is a format for publishing web content that changes regularly and is written in XML. There are many creators of free RSS feeds available online, such as Feedburner, Feedity, etc. Using RSS, you can easily publish regularly updated web content, such as blogs, articles, news, etc.

Ver artículo completo
How to generate QR codes with ASP.NET MVC?

How to generate QR codes with ASP.NET MVC?

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

Ver artículo completo

Manténgase actualizado

Obtenga excelente contenido en su bandeja de entrada todas las semanas.
Solo contenido excelente, no compartimos su correo electrónico con terceros.
Subir al inicio de la pantalla ;