In this article I will explain with an example, how to generate Barcode from database 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.
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.
Database
This article makes use of table named Products whose schema is defined as follows.
I have inserted few records in the table.
Note:You can download the database table SQL by clicking the download link below.
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
using System.Drawing.Imaging;
VB.Net
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
ImportsSystem.Configuration
Imports System.Drawing
Imports System.Drawing.Imaging
HTML Markup
The following HTML Markup consists of an ASP.Net GridView Control.
The GridView consists of two BoundField columns and a TemplateField column.
The TemplateField consists of an Image control which will be used for displaying the Barcode.
<asp:GridView runat="server" ID="gvProducts" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:BoundField HeaderText="Product Id" DataField="ProductId" />
<asp:BoundField HeaderText="Name" DataField="Name" />
<asp:TemplateField HeaderText="Barcode">
<ItemTemplate>
<asp:Image runat="server" ID="imgBarcode" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Populating GridView from Database
Inside the Page Load event handler, the GridView is populated from Products table.
C#
protected void Page_Load(object sender, EventArgse)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
protected void BindGrid()
{
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT ProductId, Name FROM Products", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
gvProducts.DataSource = dt;
gvProducts.DataBind();
}
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Protected Sub BindGrid()
Dim constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constring)
Using cmd As SqlCommand = New SqlCommand("SELECT ProductId, Name FROM Products", con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
gvProducts.DataSource = dt
gvProducts.DataBind()
End Using
End Using
End Using
End Using
End Sub
Generating and displaying Barcode image in ASP.Net
Inside the RowDataBound event handler, the Image control is referenced.
Then, the value of the ProductId is retrieved and passed to the Bitmap class which returns a Bitmap image.
Next, the length of the Barcode image is determined and the Bitmap Image is created.
Then, using the Free Barcode Font, the Barcode Graphics is drawn on the Bitmap Image.
Finally, the Bitmap Image saved into MemoryStream object and then it is converted into a BASE64 string and assigned to the Image control.
C#
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
System.Web.UI.WebControls.Image imgBarcode = (e.Row.FindControl("imgBarcode") as System.Web.UI.WebControls.Image);
string barcode = e.Row.Cells[0].Text;
using (MemoryStream ms = new MemoryStream())
{
//The Image is drawn based on length of Barcode text.
using (Bitmap bitMap = new Bitmap(barcode.Length * 40, 80))
{
//The Graphics library object is generated for the Image.
using (Graphics graphics = Graphics.FromImage(bitMap))
{
//The installed Barcode font.
Font oFont = new Font("IDAutomationHC39M Free Version", 16);
PointF point = new PointF(2f, 2f);
//White Brush is used to fill the Image with white color.
SolidBrush whiteBrush = new SolidBrush(Color.White);
graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
//Black Brush is used to draw the Barcode over the Image.
SolidBrush blackBrush = new SolidBrush(Color.Black);
graphics.DrawString("*" + barcode + "*", oFont, blackBrush, point);
}
//The Bitmap is saved to Memory Stream.
bitMap.Save(ms, ImageFormat.Png);
//The Image is finally converted to Base64 string.
imgBarcode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(ms.ToArray());
}
}
}
}
VB.Net
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim imgBarcode As System.Web.UI.WebControls.Image = (TryCast(e.Row.FindControl("imgBarcode"), System.Web.UI.WebControls.Image))
Dim barcode As String = e.Row.Cells(0).Text
Using ms As MemoryStream = New MemoryStream()
'The Image is drawn based on length of Barcode text.
Using bitMap As Bitmap = New Bitmap(barcode.Length * 40, 80)
' The Graphics library object is generated for the Image.
Using graphics As Graphics = Graphics.FromImage(bitMap)
'The installed Barcode font.
Dim oFont As Font = New Font("IDAutomationHC39M Free Version", 16)
Dim point As PointF = New PointF(2.0F, 2.0F)
'White Brush is used to fill the Image with white color.
Dim whiteBrush As SolidBrush = New SolidBrush(Color.White)
graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height)
'Black Brush is used to draw the Barcode over the Image.
Dim blackBrush As SolidBrush = New SolidBrush(Color.Black)
graphics.DrawString("*" & barcode & "*", oFont, blackBrush, point)
End Using
'The Bitmap is saved to Memory Stream.
bitMap.Save(ms, ImageFormat.Png)
'The Image is finally converted to Base64 string.
imgBarcode.ImageUrl = "data:image/png;base64," & Convert.ToBase64String(ms.ToArray())
End Using
End Using
End If
End Sub
Screenshot
Downloads