Click here to Skip to main content
11,413,676 members (69,055 online)
Click here to Skip to main content

Barcodes in ASP.NET applications

, 21 Oct 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Easy and cheap barcodes in ASP.NET.


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.


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" >
  <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" 
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <asp:Image id="myBarCode"  runat="server"></asp:Image>

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.
        /// <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);


Now, let’s see the result:


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


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Erick Cuellar
Software Developer (Senior) INTERlink Software
Guatemala Guatemala
I have been developing software for a while, using ASP.NET (since version 1.0) and C#.

Comments and Discussions

QuestionHow can i print it? Pin
badeveloper, 7-Jan-15 11:40
memberbadeveloper7-Jan-15 11:40 
QuestionNot Working Pin
SDE EDP, 3-May-14 0:20
memberSDE EDP3-May-14 0:20 
QuestionBar code generate Pin
Niranjan dotnet, 10-Apr-14 20:26
professionalNiranjan dotnet10-Apr-14 20:26 
QuestionIt is not a good idea to build barcode based on the barcode code Pin
vadim307, 13-Feb-14 20:59
membervadim30713-Feb-14 20:59 
QuestionHow to add data Pin
heemanshubhalla, 7-Feb-14 0:45
memberheemanshubhalla7-Feb-14 0:45 
QuestionNice Job Pin
Member 10186862, 21-Oct-13 22:50
memberMember 1018686221-Oct-13 22:50 
GeneralMy vote of 3 Pin
arcdon, 1-Oct-13 1:45
memberarcdon1-Oct-13 1:45 
GeneralMy vote of 1 Pin
Member 10239420, 11-Sep-13 22:20
memberMember 1023942011-Sep-13 22:20 
QuestionBarcode image is not displaying Pin
geetha rangaiah, 15-May-13 23:33
membergeetha rangaiah15-May-13 23:33 
Questioncoupon on web sites Pin
Tarcode Michael, 24-Dec-12 16:45
memberTarcode Michael24-Dec-12 16:45 
AnswerRe: coupon on web sites [modified] Pin
Member 9732330, 5-Jan-13 18:20
memberMember 97323305-Jan-13 18:20 
GeneralMy vote of 3 Pin
Sahyadri31, 5-Nov-12 1:20
memberSahyadri315-Nov-12 1:20 
QuestionScanning the barcode Pin
priti shrivastava, 29-Oct-12 23:44
memberpriti shrivastava29-Oct-12 23:44 
QuestionHow Can i use it from text box Pin
ooffooooo, 7-Sep-12 23:36
memberooffooooo7-Sep-12 23:36 
QuestionBarcode Pin
vsvarunsharma, 13-Aug-12 20:15
membervsvarunsharma13-Aug-12 20:15 
GeneralMy vote of 5 Pin
SavindraSingh, 10-Aug-12 5:12
memberSavindraSingh10-Aug-12 5:12 
GeneralMy vote of 2 Pin
Pankil_cool, 16-Jul-12 6:28
memberPankil_cool16-Jul-12 6:28 
BugBarcode not working on server but localhost Pin
Member 7952783, 11-Jun-12 23:49
memberMember 795278311-Jun-12 23:49 
GeneralMy vote of 5 Pin
manoj kumar choubey, 23-Feb-12 22:06
membermanoj kumar choubey23-Feb-12 22:06 
Questionbarcode control for Pin
Zacbr, 3-Feb-12 15:06
memberZacbr3-Feb-12 15:06 
Questionhow can I do to see the bar code in webform [modified] Pin
Member 8509294, 22-Dec-11 22:32
memberMember 850929422-Dec-11 22:32 
AnswerRe: how can I do to see the bar code in webform Pin
steve7g, 10-Jan-12 17:34
membersteve7g10-Jan-12 17:34 
QuestionBarcode for Crystal Report Pin
Member 8218467, 6-Sep-11 23:34
memberMember 82184676-Sep-11 23:34 
AnswerRe: Barcode for Crystal Report Pin
Ivy Shirley, 24-Nov-11 20:27
memberIvy Shirley24-Nov-11 20:27 
QuestionBarCodes Generated Fine But Not being READ Pin
The nk, 4-Aug-11 2:43
memberThe nk4-Aug-11 2:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150427.2 | Last Updated 21 Oct 2008
Article Copyright 2008 by Erick Cuellar
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid