In this article I will explain with an example, how to read QRCodeCode Image in ASP.Net Core (.Net Core 3) MVC.
Note: For beginners in ASP.Net Core (.Net Core 3) MVC, please refer my article ASP.Net Core 3: Hello World Tutorial with Sample Program example.
 
 

Installing System.Drawing.Common library

First, you will need to install the System.Drawing.Common package.
Note: For more details, please refer my article How to use Bitmap class in .Net Core 3.1.
 
 

Install ZXing.Net package using Nuget

In order to install ZXing.Net library using Nuget, please refer my article Install ZXing.Net library using Nuget.
 
 

Namespaces

You will need to import the following namespaces.
using System.IO;
using System.Drawing;
using ZXing;
using ZXing.Common;
 
 

Controller

The Controller consists of following Action methods.

Action method for handling GET operation

Inside this Action method, simply the View is returned.
 

Action method for handling POST operation

This Action method gets called, when a File is selected and the Read Button is clicked.
Inside this Action method, the selected file is received in the IFormFile as parameter.
Note: In case the IFormFile parameter is appearing NULL, then please refer my article, ASP.Net Core: IFormFile always returns NULL.
 
Then, an object of MemoryStream class is created and the selected file is copied into it and a BitMap class object is created and object of MemoryStream is saved in it.
After that, Bitmap is saved in the BitmapLuminanceSource object and then, the source is converted to BinaryBitmap object.
Finally, the QR Code is read using decode method of MultiFormatReader class and the decoded value is set into ViewBag object.
public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
 
    [HttpPost]
    public IActionResult Index(IFormFile postedFile)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            // Create Bitmap from selected file.
            postedFile.CopyTo(ms);
            using (Bitmap bitMap = new Bitmap(ms))
            {
                //Read the QRCode Image.
                BitmapLuminanceSource source = new BitmapLuminanceSource(bitMap);
                BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));
 
                // Reader object accept BinaryBitmap and decode it.
                Result result = new MultiFormatReader().decode(binaryBitmap);
                ViewBag.QRText = result.Text;
            }
        }
 
        return View();
    }
}
 
 

View

HTML Markup

The View consists of an HTML Form created with following ASP.Net Tag Helpers attributes.
asp-action – Name of the Action. In this case the name is Index.
asp-controller – Name of the Controller. In this case the name is Home.
method – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
The Form element has been specified with enctype attribute and its value must be multipart/form-data, otherwise the IFormFile parameter will be NULL.
Inside the HTML Form there is an HTML FileUpload, a Submit Button and a SPAN element.
 

Submitting the Form

When the Submit Button is clicked, the ViewBag object is checked for NULL and if it is not NULL then the value of the ViewBag is displayed using SPAN element.
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <form method="post" enctype="multipart/form-data" asp-controller="Home" asp-action="Index">
        <input type="file" name="postedFile" />
        <input type="submit" id="btnRead" value="Read" />
        <br /><br />
        @if (ViewBag.QRText != null)
        {
            <span>@ViewBag.QRText</span>
        }
    </form>
</body>
</html>
 
 

Screenshot

ASP.Net Core 3.1: Read QRCode Image
 
 

Downloads