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.
3. After installation is completed, restart your machine.
HTML Markup
The HTML Markup consists of following controls:
TextBox – For capturing the user input.
The Button has been assigned with an OnClick event handler.
<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
When the Generate Button is clicked,
TextBox value is stored and
Bitmap and
Graphics class objects are created using the Free
Barcode Font and the
Barcode Graphics is drawn on the
Bitmap Image.
Then, Font class object is created where the version is defined and PonitF, SolidBrush class object is created using which the colors are set.
The MemoryStream class object is created and Bitmap Image saved into MemoryStream object and is converted into a BASE64 string and assigned to the ImageUrl property of Image control.
Finally, the Visible property of Image control is set 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))
{
using (Font font = 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 + "*", font, 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(ByVal sender As Object, ByVal e As EventArgs)
Dim barCode As String = txtBarcode.Text
Using bitMap As Bitmap = New Bitmap(barCode.Length * 40, 80)
Using graphics As Graphics = graphics.FromImage(bitMap)
Using font As Font = New Font("IDAutomationHC39M Free Version", 16)
Dim point As PointF = New PointF(2.0F, 2.0F)
Dim blackBrush As SolidBrush = New SolidBrush(Color.Black)
Dim whiteBrush As SolidBrush = New SolidBrush(Color.White)
graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height)
graphics.DrawString("*" & barCode & "*", font, blackBrush, point)
End Using
End Using
Using ms As MemoryStream = 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
Demo
Downloads