Asp.Net Core Tutorial: Barcode Generation with C#.Net

Step‑by‑step ASP.NET Core mvc tutorial on barcode generation with C#.Net. Code examples, and best practices to create and customize barcode.
In today's article I will show you a simple tutorial how you can generate barcode with in you asp.net 
core mvc application using c#.net. 

Please check my previous article ASP.NET Core Tutorial: Generate QR Codes with C#.NET

You may also like these articles:

In this article we will use asp.net core 10 mvc application with C#.net. So first we will install NuGet package named as NetBarCode.

NetBarCode NuGet Package

Now open the controller and install the above mentioned NuGet package "NetBarCode". Add a controller file and add the below code in it. 
using Microsoft.AspNetCore.Mvc;
using NetBarcode;
namespace BarCode.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            var barcode = new Barcode("123456789", true);
            string savePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "barcode.png");
            barcode.SaveImageFile(savePath, ImageFormat.Png);
            return View();
        }
    }
} 
In above code i have created object for BarCode and added value. This value is the data for which we are going to generate the barcode. In this if you want to display the barcode value also you need to pass true. In next line i have saved the barcode image to wwwroot folder. Now create a view and add the below code in it.
@{
    ViewData["Title"] = "Bar Code";
}

<div style="text-align:left;">
    <img src="barcode.png" style="width:200px;height:200px" />
</div>  
In above code I have bind the generated barcode image to img tag. Now we have done. Run the code to check the output.
Barcode
BarCode.zip 20KiB

Post a Comment