how to scan QR code in asp net c# Archives - Tech Insights Unveiling Tomorrow's Tech Today, Where Innovation Meets Insight Tue, 03 Jan 2023 15:21:12 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.2 https://i0.wp.com/reactconf.org/wp-content/uploads/2023/11/cropped-reactconf.png?fit=32%2C32&ssl=1 how to scan QR code in asp net c# Archives - Tech Insights 32 32 230003556 How to Generate QR Code using Asp.net Core C# https://reactconf.org/how-to-generate-qr-code-using-asp-net-core-c/ https://reactconf.org/how-to-generate-qr-code-using-asp-net-core-c/#respond Tue, 03 Jan 2023 15:21:12 +0000 https://labpys.com/?p=618 In this article, we will learn how to Generate a QR Code using Asp.net Core MVC C#. In the previous article, we discussed https://labpys.com/how-to-export-data-from-database-table-to-excel-file-in-asp-net-core-mvc. Prerequisites Create an ASP.NET Core MVC …

The post How to Generate QR Code using Asp.net Core C# appeared first on Tech Insights.

]]>
In this article, we will learn how to Generate a QR Code using Asp.net Core MVC C#. In the previous article, we discussed https://labpys.com/how-to-export-data-from-database-table-to-excel-file-in-asp-net-core-mvc.

Prerequisites

  • Download and install .Net Core 6.0 SDK from here
  • Download and Install Microsoft Visual Studio 2022 from here

Create an ASP.NET Core MVC Project

Open Visual Studio, Go to the File menu click on New, and select Project. Then new project window, select the ASP.NET Core Web Application (Model-View-Controller) template.

Enter a project name and click Create.

Install NuGet Packages for QR Code Generator

  • QRCoder

Create Controller

using Microsoft.AspNetCore.Mvc;
using QRCoder;
using System.Drawing;
using System.Drawing.Imaging;

namespace Asp.netcore_Tutorials.Controllers
{
    public class QRCodeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        [AutoValidateAntiforgeryToken]
        public IActionResult Index(string QRtext)
        {
            QRCodeGenerator QRGen = new QRCodeGenerator();
            QRCodeData Qrinfo = QRGen.CreateQrCode(QRtext, QRCodeGenerator.ECCLevel.Q);
            QRCode qRCoder = new QRCode(Qrinfo);

            Bitmap QRbitmap = qRCoder.GetGraphic(50);

            // Color 
            //Bitmap QRbitmap = qRCoder.GetGraphic(50, Color.Blue, Color.Gray, true);

            byte[] bitmapArray = bitmaptoArray(QRbitmap);
            var Qrcodeimage= string.Format("data:image/png;base64,{0}", Convert.ToBase64String(bitmapArray));
            ViewBag.QRCodeImage = Qrcodeimage;
            return View();
        }

        private static byte[] bitmaptoArray(Bitmap bitmapimage)
        {
            using (MemoryStream mstream = new MemoryStream())
            {

                bitmapimage.Save(mstream, ImageFormat.Png);
                return mstream.ToArray();
            }

        }
    }
}

Customize View Index

  
@{
    ViewData["Title"] = "Home Page";
}

<div class="row">
    <div class="col-md-4">
        <form  asp.asp-controller="QRCode" asp-action="Index">
            <div class="form-group">
                <label   class="control-label">QR Code Text</label> 
                <input name="QRtext" class="form-control" />
            </div>
            <div class="form-group">
                <input type="submit" value="Generate QRCode" class="btn btn-primary" />
            </div>
            <div class="form-group">
                <img src="@ViewBag.QRCodeImage" alt="" class="img-thumbnail" />               
            </div> 
        </form>
    </div>
</div>

The post How to Generate QR Code using Asp.net Core C# appeared first on Tech Insights.

]]>
https://reactconf.org/how-to-generate-qr-code-using-asp-net-core-c/feed/ 0 618