How To Generate Barcode In ASP.NET Using C#

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.