Click here to Skip to main content
Email Password   helpLost your password?

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
{

/// <summary />
/// Summary description for BarCode.
/// </summary />

public class BarCode : System.Web.UI.Page
{
    private void Page_Load(object sender, System.EventArgs e)
    {
         // Get the Requested code to be created.
         string Code = Request["code"].ToString();

         // Multiply the lenght of the code by 40 (just to have enough width)
         int w = Code.Length * 40;
    
        // Create a bitmap object of the width that we calculated and height of 100
         Bitmap oBitmap = new Bitmap(w,100);
    
        // then create a Graphic object for the bitmap we just created.
         Graphics oGraphics = Graphics.FromImage(oBitmap);
        
        // Now create a Font object for the Barcode Font
        // (in this case the IDAutomationHC39M) of 18 point size
        Font oFont = new Font("IDAutomationHC39M", 18);
        
        // Let's create the Point and Brushes for the barcode
        PointF oPoint = new PointF(2f, 2f);
         SolidBrush oBrushWrite = new SolidBrush(Color.Black);
         SolidBrush oBrush = new SolidBrush(Color.White);
            
        // Now lets create the actual barcode image
        // with a rectangle filled with white color
        oGraphics.FillRectangle(oBrush, 0, 0, w, 100);
            
        // We have to put prefix and sufix of an asterisk (*),
        // in order to be a valid barcode
        oGraphics.DrawString("*" + Code + "*", oFont, oBrushWrite, oPoint);
        
        // Then we send the Graphics with the actual barcode
        Response.ContentType = "image/jpeg" ;
         oBitmap.Save (Response.OutputStream, ImageFormat.Jpeg);
    }
}
}

Let's test our page. Just call http://localhost/BarCodes/BarCode.aspx?Code=1234.

Pic1.png

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
{
    /// <summary />
    /// Summary description for WebForm1.
    /// </summary />
    public class WebForm1 : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Image myBarCode;
    
        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
            myBarCode.ImageUrl = "BarCode.aspx?code=31231";
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }
        
        /// <summary />
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary />
        private void InitializeComponent()
        {    
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion
    }
}

Now, let’s see the result:

Pic2.png

Well friends, that is all! An easy and cheap way to have barcodes in your ASP.NET applications.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Generalbarcode is not showing the image
gulam husain ansari
22:17 14 Sep '09  
hi this is very helpfull...
please help me as soon as possible
i am trying to mage the barcode
i have follow the above procedure but it is given only the *31231*
i need the bar code and below this the *31231* but it is not given the barcode only given the *31231* please help me to make this

thank you
AnswerRe: barcode is not showing the image
Erick Cuellar
14:27 25 Oct '09  
Hi, did you install the font before?
QuestionSolid black bar
holderag
3:23 10 Jun '09  
Hi,
This looks great, but when I try to view, I get just a solid black rectangle.

Thanks for any help you can provide.

ahp
General"Real" Free Barcode Font
Mad-Halfling
7:13 21 Apr '09  
The font linked to in the article isn't 100% free for commercial use. However this one _is_ and appears to work
http://www.barcodesinc.com/free-barcode-font/[^]

Note that after you install the font, make sure you don't have any of the dev web-server engines running or they may not pick up the new font.
The font name line is

Font oFont = new Font("Free 3 of 9", 40);

(I've set my font size a bit larger)
QuestionI'm getting only the label (*1234*) not the bar code lines. What is the issue?. Please help me
arunkumarsp
1:34 13 Apr '09  
I'm getting only the label (*1234*) not the bar code lines. What is the issue?. Please help me
GeneralBarcode work
Sreepappu
21:53 30 Nov '08  
Hi,
Good job.Using the code,I have created the image.When I try to read the Code from Barcode scanner,Its not reading.How to read.More thankful
to answer this.
GeneralGood Work
Manickarj
20:26 28 Oct '08  
This is very useful for Beginners.
GeneralNice!
Dinesh Mani
1:25 23 Oct '08  
Nice way to print bar codes. I understand that this will work client machines that don't have the barcode font installed.

But isn't it a lot easier to just put the data in a div and set the div's font to the bar code font? It won't render the bar code on all the client machines, but u can embed the font and get it rendered as expected on the client side! So executing a new page just for the purpose of rendering a text in a specified font, I think, is not that worthy. And if you use this in a high transaction web site, you will be unnecessarily increasing the load on the web server.

Just a suggestion.

4* s from me for the idea though!
GeneralRe: Nice!
MontereySoft
14:37 7 Feb '09  
Font is easy to use, but you have to install it on every computer to display the barcode. A better choice is to create barcode images at the server side. We use Morovia Barcode ActiveX and have great success. An example here: http://www.morovia.com/activex/active-demo/test-label.asp[^]


Last Updated 21 Oct 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010