Hi rani,
Using below article i have created the example.
Check this example. Now please take its reference and correct your code.
Namespaces
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using QRCoder;
Controller
public class HomeController : Controller
{
public IActionResult Index()
{
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.AddRange(new System.Data.DataColumn[] {
new System.Data.DataColumn("Year", typeof(string)),
new System.Data.DataColumn("ToBill",typeof(decimal)),
new System.Data.DataColumn("Billed",typeof(decimal))});
dt.Rows.Add(2011, 1000, 500);
dt.Rows.Add(2012, 2500, 100);
dt.Rows.Add(2013, 100, 50);
dt.Columns.Add("Percentage");
dt.Columns.Add("QRCode");
for (int i = 0; i < dt.Rows.Count; i++)
{
System.Data.DataRow dr = dt.Rows[i];
dr["Percentage"] = Math.Round((Convert.ToDecimal(dr["Billed"]) / Convert.ToDecimal(dr["ToBill"])) * 100);
dr["QRCode"] = GenereteQRCodeImage(dr["Year"].ToString());
}
ViewBag.Data = dt;
return View();
}
private string GenereteQRCodeImage(string qrcode)
{
using (MemoryStream ms = new MemoryStream())
{
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode(qrcode, QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
using (Bitmap bitMap = qrCode.GetGraphic(20))
{
bitMap.Save(ms, ImageFormat.Png);
return "data:image/png;base64," + Convert.ToBase64String(ms.ToArray());
}
}
}
}
View
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<table>
<tr>
@for (int column = 0; column < ViewBag.Data.Columns.Count - 1; column++)
{
<th>@ViewBag.Data.Columns[column].ColumnName</th>
}
<th></th>
</tr>
@for (int row = 0; row < ViewBag.Data.Rows.Count; row++)
{
<tr>
@for (int column = 0; column < ViewBag.Data.Columns.Count - 1; column++)
{
<td>@ViewBag.Data.Rows[row][column]</td>
}
<td><img src="@ViewBag.Data.Rows[row]["QRCode"]" alt="@ViewBag.Data.Rows[row]["Year"]" height="100" width="100" /></td>
</tr>
}
</table>
</body>
</html>
Screenshot