Generate and display barcode image in HTML Table row in ASP.Net Core MVC

Last Reply 12 days ago By dharmendr

Posted 12 days ago

How to display barcode in table row in asp.net core mvc 

The bar code will be generated and displayed in a column.

You are viewing reply posted by: dharmendr 12 days ago.
Posted 12 days ago

Hi rani,

Using below article i have created the example. 

ASP.Net Core: Dynamically generate and display Barcode Image in ASP.Net MVC Core

Check this example. Now please take its reference and correct your code.

Namespaces

using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

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.Rows.Add(2014, 1000, 500);
        dt.Rows.Add(2015, 400, 200);
        dt.Rows.Add(2016, 3000, 2000);
        dt.Rows.Add(2017, 2500, 100);
        dt.Rows.Add(2018, 100, 50);
        dt.Rows.Add(2019, 2500, 100);
        dt.Rows.Add(2020, 100, 50);

        dt.Columns.Add("Percentage");
        dt.Columns.Add("Barcode");
        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["Barcode"] = GenereteBarcodeImage(dr["Year"].ToString());
        }

        ViewBag.Data = dt;

        return View();
    }

    private string GenereteBarcodeImage(string barcode)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            using (Bitmap bitMap = new Bitmap(barcode.Length * 40, 80))
            {
                using (Graphics graphics = Graphics.FromImage(bitMap))
                {
                    Font oFont = new Font("IDAutomationHC39M Free Version", 16);
                    PointF point = new PointF(2f, 2f);
                    SolidBrush whiteBrush = new SolidBrush(Color.White);
                    graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
                    SolidBrush blackBrush = new SolidBrush(Color.Black);
                    graphics.DrawString("*" + barcode + "*", oFont, blackBrush, point);
                }

                bitMap.Save(ms, ImageFormat.Png);
                return "data:image/png;base64," + Convert.ToBase64String(ms.ToArray());
            }
        }
    }
}

View

@using System.Data;
@{
    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]["Barcode"]" alt="@ViewBag.Data.Rows[row]["Year"]" /></td>
            </tr>
        }
    </table>
</body>
</html>

Screenshot