In this article I will explain with an example, how to generate
QRCode with Logo (Image) in center in ASP.Net Core (.Net Core) MVC.
Installing QRCoder package and System.Drawing.Common using Nuget
Logo Location
The Logo image is saved in the Folder (Directory) named images of wwwroot Folder (Directory).
Namespaces
You will need to import the following namespaces.
using QRCoder;
using System.Drawing;
using System.Drawing.Imaging;
Controller
First, a private property of IWebHostEnvironment interface is created and the IWebHostEnvironment interface is passed as parameter to Constructor of HomeController.
Inside the Constructor, the private property is set with the instance of the IWebHostEnvironment accepted as parameter.
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 accepts the TextBox value as a parameter.
Inside this Action method, the QRCode object is generated using the text provided by the User with the help of QRCodeGenerator and QRCode class.
Then, two Bitmaps are created, one for the QRCode and another for the Logo Image.
Note: The Logo Image is read from the
Images folder within the Project directory (discussed earlier) using
IWebHostEnvironment interface.
After setting the resolution, the Coordinates for drawing the Logo Image in the center of the QRCode are calculated and the Logo Image is drawn in the center of the QRCode Bitmap.
Finally, the Bitmap is saved to the
MemoryStream and converted into BASE64 string which is later set to the
ViewBag object and the page is redirected to
Index View.
public class HomeController : Controller
{
private IWebHostEnvironment Environment {get; set;}
public HomeController(IWebHostEnvironment environment)
{
this.Environment = environment;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Generate(string qrcode)
{
//Generate QRCode object.
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCode qrCode = new QRCode(qrGenerator.CreateQrCode(qrcode, QRCodeGenerator.ECCLevel.Q));
//Create a Bitmap using the QRCode.
using (Bitmap bmpQRCode = qrCode.GetGraphic(30))
{
//Fetch file name of the Logo Image.
string fileName = Path.Combine(this.Environment.WebRootPath, "images/ASPSnippets.png");
//Read the Logo Image.
using (Bitmap bmpLogo = new Bitmap(fileName)
{
//Set the Resolution.
bmpLogo.SetResolution(80f, 80f);
//Calculating the coordinates for the Logo Image.
int x = (bmpQRCode.Height - bmpLogo.Height) / 2;
int y = (bmpQRCode.Width - bmpLogo.Width) / 2;
Point p = new Point(x, y);
//Drawing the Logo Image in the center of the QRCode.
Graphics graphics = Graphics.FromImage(bmpQRCode);
graphics.DrawImage(bmpLogo, p);
}
using (MemoryStream ms = new MemoryStream())
{
//Saving the QRCode Bitmap to MemoryStream.
bmpQRCode.Save(ms, ImageFormat.Png);
//Displaying the QRCode Image using BASE64 string.
ViewBag.QRCodeImage = "data:image/png;base64," + Convert.ToBase64String(ms.ToArray());
}
}
return View("Index");
}
}
View
HTML Markup
The View consists of an HTML Form with following ASP.Net Tag Helpers attributes.
asp-action – Name of the Action. In this case the name is Generate.
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 View consists of an HTML TextBox, a Submit Button and an Image 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 object is set into the
src property of the Image element and the generated QRCode Image is displayed.
@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" asp-action="Generate" asp-controller="Home">
<input type="text" name="qrcode" />
<input type="submit" value="Generate" />
</form>
<br />
@if (ViewBag.QRCodeImage != null)
{
<img src="@ViewBag.QRCodeImage" alt="" style="height:150px;width:150px" />
}
</body>
</html>
Screenshot
Demo
Downloads