In this article I will explain with an example, how to dynamically generate and display Barcode Image in ASP.Net using C# and VB.Net.
The Barcode Image will be generated using the IDAutomationHC39M Free Version Barcode Font in ASP.Net using C# and VB.Net.
Note: The IDAutomationHC39M Free Version Barcode Font is provided by IDAutomation and they are the creators and owners of the Font and its license.
 
 
Downloading and installing Barcode Font
First you will need to download the Free Barcode Font from the following link.
Once downloaded follow the following steps.
1. Extract the Font from the ZIP file.
2. Double Click, Open the File and then click the Install Button as shown below.
Dynamically generate and display Barcode Image in ASP.Net
 
3. After installation is completed, restart your machine.
 
 

HTML Markup
The HTML Markup consists of an ASP.Net TextBox, a Button and an Image control.
The barcode entered in the TextBox will be converted to an Image Barcode and then later displayed in the Image control.
<asp:TextBox ID="txtBarcode" runat="server"></asp:TextBox>
<asp:Button ID="btnGenerate" runat="server" Text="Generate" OnClick="GenerateBarcode" />
<hr />
<asp:Image ID="imgBarcode" runat="server" Visible="false" />
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
 
VB.Net
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Imaging
 
 
Dynamically generating and displaying Barcode Image in ASP.Net
Inside the GenerateBarcode event handler, first a Bitmap Image is created and then using the Free Barcode Font, the Barcode Graphics is drawn on the Bitmap Image.
Then the Bitmap Image saved into MemoryStream object and then it is converted into a BASE64 string and assigned to the Image control.
Finally, the Image control is made visible by setting the Visible property to True.
C#
protected void GenerateBarcode(object sender, EventArgs e)
{
    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);
            imgBarcode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(ms.ToArray());
            imgBarcode.Visible = true;
        }
    }
}
 
VB.Net
Protected Sub GenerateBarcode(sender As Object, e As EventArgs)
    Dim barCode As String = txtBarcode.Text
    Using bitMap As New Bitmap(barCode.Length * 40, 80)
        Using graphics__1 As Graphics = Graphics.FromImage(bitMap)
            Dim oFont As New Font("IDAutomationHC39M Free Version", 16)
            Dim point As New PointF(2.0F, 2.0F)
            Dim blackBrush As New SolidBrush(Color.Black)
            Dim whiteBrush As New SolidBrush(Color.White)
            graphics__1.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height)
            graphics__1.DrawString("*" & barCode & "*", oFont, blackBrush, point)
        End Using
        Using ms As New MemoryStream()
            bitMap.Save(ms, ImageFormat.Png)
            imgBarcode.ImageUrl = "data:image/png;base64," & Convert.ToBase64String(ms.ToArray())
            imgBarcode.Visible = True
        End Using
    End Using
End Sub
 
 
Screenshot
Dynamically generate and display Barcode Image in ASP.Net
 
 
Demo
 
 
Downloads