Introduction
In this blog, we will learn how to generate a barcode using ASP.Net by simply entering Text, numbers, and a combination of both in the textbox and click on generate button. I found a very useful library for generating barcodes which is IronBarcode.
You can install it through the NuGet package. It supports .Net Core, Standard & Framework. It has cross-platform support.
To use this library we will create a demo project in a Visual Studio. I am creating ASP.Net Web Application using Web Form for that I am going to use the Visual Studio 2017 version
Creating a New Project in Visual Studio
Open the Microsoft Visual Studio software and go to the File menu. Select "New Project", and then select "Web Application". In this article, we are going to use a web application to generate barcodes.

Select “Web” from the right panel then select ASP.NET Web Application (.NET Framework). Enter the project name. I am giving GenerateBarcode_Demo. You can give anything you wish to, and select the file path in the appropriate text box. I am choosing default, and selecting the required Dot net Framework, as in the screenshot below. Then, click the “OK” button, as in the screenshot below.

Then select the “Empty” template and check the “Web Form” checkbox from “Add folders and core reference for” then click on the “OK” button as shown in the below screenshot.

The Microsoft Visual Studio project will now generate the structure for the selected application, and In this example, we are using ASP.Net Web Form So we can add a new item and then choose web from to write the code where you can enter the code and build and run the application. Next, we can add the library to test the code.
How to Install the Barcode Library through NuGet Package Manager
In Visual Studio Select Tools > NuGet Package Manager > Manage NuGet Packages for the solution.

Search for the specific package IronBarcode using the search box on the upper left. Select a package from the list to display its information, enabling the Install button and a version-selection drop-down. As shown below screenshot. The NuGet package will be installed for your project and reference will be added.

Advanced barcode creation
We can add the annotation to the barcode, set them, set the font, display its value below it, add margins, change the barcode color, and then save it, all quite simply in C#. We can also choose to export to HTML or PDF instead of an image if that is more appropriate for our application.
// Styling a QR code and adding annotation text
barcode.AddBarcodeValueTextBelowBarcode();
barcode.AddBarcodeValueTextAboveBarcode();
barcode.SetMargins(10);
barcode.ChangeBarCodeColor(Color.BlueViolet);
Design HTML Web From GenerateBarcode.
Drag and drop TextBox, Button, and Image control from the Visual Studio toolbox.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GenerateBarcode.aspx.cs" Inherits="GenerateBarcode.GenerateBarcode" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="lib/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<form id="form1" runat="server">
<div class="container">
<h2>Generate QR Images in C#</h2>
<div class="row">
<div class="col-4">
<div class="form-group">
<label>Enter a value to generate barcode:</label>
<div class="input-group">
<asp:TextBox ID="txtGenerateBarcode" runat="server" CssClass="form-control"></asp:TextBox>
<div class="input-group-append">
<asp:Button ID="BtnGenerateBarcode" runat="server" Text="Generate Barcode" CssClass="btn btn-primary" OnClick="BtnGenerateBarcode_Click" />
</div>
</div>
</div>
</div>
</div>
<asp:Image ID="ImageGeneratedBarcode" runat="server" CssClass="img-thumbnail" Visible="false"/>
</div>
</form>
</body>
</html>
Double click or enter Button control. It will create an event in code behind aspx.cs file
Write the following code,
Add namespace
using IronBarCode;
using System;
using System.Drawing;
using System.IO;
namespace GenerateBarcode
{
public partial class GenerateBarcode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ImageGeneratedBarcode.Visible = false;
}
protected void BtnGenerateBarcode_Click(object sender, EventArgs e)
{
string generatebarcode = txtGenerateBarcode.Text;
GeneratedBarcode barcode = BarcodeWriter.CreateBarcode(generatebarcode, BarcodeWriterEncoding.Code128);
barcode.ResizeTo(400, 120);
// Styling a QR code and adding annotation text
//barcode.AddBarcodeValueTextAboveBarcode();
barcode.AddBarcodeValueTextBelowBarcode();
barcode.SetMargins(10);
barcode.ChangeBarCodeColor(Color.BlueViolet);
var folder = Server.MapPath("/App_Data/GeneratedBarcodeImage");
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
string filePath = Server.MapPath("GeneratedBarcodeImage/barcode.png");
barcode.SaveAsPng(filePath);
ImageGeneratedBarcode.ImageUrl = "~/GeneratedBarcodeImage/" + Path.GetFileName(filePath);
ImageGeneratedBarcode.Visible = true;
}
}
}
Run project Ctrl+F5

Conclusion
The IronBarcode is also helpful in creating Barcode Images, Barcode HTML, and Barcode PDF formate. Using Barcode we can create different types of images file like png, jpg, Gif, etc. The code of this tutorial is attached. You can download the code, play with the code and modify it according to your project requirement. Thank you for reading. Happy coding.

Abhilash ChalisseryPosted Jul 5, 2021, 8:18 AM
There is no barcode just its showing *1223456* for the barcode. It seems like not only my issue here. I guess we have wasted out time by taking this reference.
Craig PozenPosted Mar 26, 2020, 9:01 PM
Same issue. what dll are we supposed to reference? I can't see any barcode just the numbers
Sanoop HariPosted Aug 2, 2019, 12:38 AM
Its only working locally, when i moved to live only image with number is showing ,no barcode is there and also the generated barcode will not get any data while scanning.
Ashu shahPosted Jun 20, 2019, 6:47 AM
I have add dll file also
Ashu shahPosted Jun 20, 2019, 6:46 AM
It only generate number not barcode...please help
yallaling kumbharPosted Jun 18, 2019, 2:21 AM
Not working
saiful islamPosted May 22, 2019, 1:29 AM
This only generate number not bar-code.
Ravi RajPosted Dec 18, 2018, 3:20 AM
Working only in localhost. how can it will on live mode
chandresh kalsariyaPosted Sep 11, 2018, 11:42 PM
This solution is perfectly run in localhost but when this code on the server is not working.
chandresh kalsariyaPosted Aug 16, 2018, 9:49 AM
But is not working.
chandresh kalsariyaPosted Aug 16, 2018, 8:10 AM
This bar-code is not working I think you put star at end of the string . it's working properly and can you tell me please how to generate barcode in web application ?