Introduction
In one of my projects, I needed to implement barcodes into an ASP.NET page. Unfortunately, the only way that I found was using a third party component. So, I decided to find a way to do it without using external components.
Using the code
First, you have to download a free barcode font. For this example, I used "IDAutomationHC39M" from IdAutomation. In this example, I used Barcode 39.
In WinForms applications, it is really easy to use Barcode fonts; just place a Label
, and apply the free barcode font, and assign a value, and everything is ready.
In Webforms, things are different, because the application runs on the server. The barcode font must reside on the server. If we use a Label, the barcode font is located on the server, not on the client, so you will just see the value, not the barcode.
Well, let's start:
Just copy the barcode font into the windows\fonts folder of the server. The whole idea is to create a text with the font (a barcode), then create an image of it, and send it back to the client.
Here is a simple page with it:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Barcodes
{
public class BarCode : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
string Code = Request["code"].ToString();
int w = Code.Length * 40;
Bitmap oBitmap = new Bitmap(w,100);
Graphics oGraphics = Graphics.FromImage(oBitmap);
Font oFont = new Font("IDAutomationHC39M", 18);
PointF oPoint = new PointF(2f, 2f);
SolidBrush oBrushWrite = new SolidBrush(Color.Black);
SolidBrush oBrush = new SolidBrush(Color.White);
oGraphics.FillRectangle(oBrush, 0, 0, w, 100);
oGraphics.DrawString("*" + Code + "*", oFont, oBrushWrite, oPoint);
Response.ContentType = "image/jpeg" ;
oBitmap.Save (Response.OutputStream, ImageFormat.Jpeg);
}
}
}
Let's test our page. Just call http://localhost/BarCodes/BarCode.aspx?Code=1234.
As you can see, there is a barcode with the value of 1234.
Now, let’s create a page that has an asp:Image
on it:
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="BarCodes.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Image id="myBarCode" runat="server"></asp:Image>
</form>
</body>
</HTML>
Add this to the code behind:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace BarCodes
{
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Image myBarCode;
private void Page_Load(object sender, System.EventArgs e)
{
myBarCode.ImageUrl = "BarCode.aspx?code=31231";
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
Now, let’s see the result:
Well friends, that is all! An easy and cheap way to have barcodes in your ASP.NET applications.