csharp

Generate Barcode In ASP.NET

In this article we will learn with an example how to generate barcode In ASP.NET.

In this blog, we will learn how to generate a barcode in ASP.NET by entering numbers and alphabet letters in the textbox and clicking Generate button.

Generate Barcode In ASP.NET

Step 1. First we will need to download the Free Barcode Font from the given below link.
Please Download Barcode

Step 2. First Extract the zip file and install on your system. Open the file and then click on Install button like screenshot. After installation, restart your system.

generate-barcode-in-asp-net

Step 3. Create a project in the Visual Studio. Give it a meaningful name.

Step 4.In this step we will add web form right on the project from solution explorer, add new item, choose web form and give it a name as per your choice.

Step 5. Design HTML web form

write this code on .aspx page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="BarCode.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style>
   .txtBarcode {
  width: 20%;
  padding: 12px 20px;
  margin: 8px 0;
  display: inline-block;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
}
.btnGenerateBarCode:hover {
  background-color: #45a049;
}
.btnGenerateBarCode {
  width: 10%;
  background-color: #4CAF50;
  color: white;
  padding: 14px 20px;
  margin: 8px 0;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}
.div1 {
  border-radius: 5px;
  background-color: #f2f2f2;
  padding: 20px;
}
    </style>
</head>
    
<body>
    <form id="form1" runat="server">
   <div id="div1" class="div1">
           <h2 class="text-center text-uppercase">How to create barcode in asp.net</h2>  
             <label for="fname">Enter Value :</label> 
            <asp:TextBox ID="txtBarcode" CssClass="txtBarcode"  runat="server"></asp:TextBox>
            <asp:Button ID="btnGenerateBarCode" runat="server" CssClass="btnGenerateBarCode" Text="Generate" OnClick="btnGenerateBarCode_Click" />
            <hr />
           
            <asp:Image ID="imgBarcode" runat="server" Visible="false" />
             <hr />
        </div>
           
    </form>
</body>
</html>

Step 6. Add Namespaces on .aspx.cs page.

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

Generate Barcode In ASP.NET
Write this following code on .aspx.cs page. Inside the btnGenerateBarCode_Click event handler calling one function GenerateBarCode()

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace BarCode
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnGenerateBarCode_Click(object sender, EventArgs e)
        {
            GenerateBarCode();
        }
        public void GenerateBarCode()
        {
            string barCode = txtBarcode.Text;
            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 blackBrush = new SolidBrush(Color.Black);
                    SolidBrush whiteBrush = new SolidBrush(Color.White);
                    graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
                    graphics.DrawString("*" + barCode + "*", oFont, blackBrush, point);
                }
                using (MemoryStream ms = new MemoryStream())
                {
                    bitMap.Save(ms, ImageFormat.Png);
                    byte[] byteImage = ms.ToArray();
                    imgBarcode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
                    imgBarcode.Visible = true;
                }
            }
        }
    }
}

Output

generate-barcode-in-asp-net

Leave a Reply

Your email address will not be published. Required fields are marked *