Loading ...

Generate BarCode in ASP.NET | CodeAsp.Net

Generate BarCode in ASP.NET

 /5
0 (0votes)

Barcode is a machine readable representation of data, which contains the data information. Basically barcode contains data information in parallel line. The most popular of barcode used font is “3 of 9” also known a code 39.

Download Font

Download barcode from above link and exact these in a folder after that just copy and paste font on the following location Control Panel >> Font.

Basically during barcode generation we first create a text with the help of font in parallel lines, after that we create an image of this text.

Following is the Code snippet for barcode generation:

public static void CreateBarCode(string barCodedDetail,string fontName,int fontSize, string physicalPath) 

 {


 //Find the Width for barcode

 int width = barCodedDetail.Length*35; 

 

 //create Bitmap object with Width and Height

 Bitmap barCode = new Bitmap(width, 120); 

 

 //path where you want to save the barcode Image

 string filePath = string.Format("{0}{1}.png", physicalPath, barCodedDetail);

 

 //create the barcoded font object

 Font barCodeFont = new Font(fontName, fontSize, FontStyle.Regular, GraphicsUnit.Point);

 

 //creating the graphics object for the Bitmap.

  Graphics graphics = Graphics.FromImage(barCode);

 

 SizeF sizeF = graphics.MeasureString(barCodedDetail, barCodeFont); 

 

 barCode = new Bitmap(barCode, sizeF.ToSize()); 

 

 graphics = Graphics.FromImage(barCode);

 

 SolidBrush brushBlack = new SolidBrush(Color.Black);

 

graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;

 

 //putting * before and after the barCodedDetail,

 //this is because scanner only read the data which is started and end with *

 graphics.DrawString("*" + barCodedDetail + "*", barCodeFont, brushBlack, 1,1); 

 

 graphics.Dispose(); 

 //Saving the Image file

 barCode.Save(filePath, ImageFormat.Png);

  barCode.Dispose();  

 HttpContext.Current.Response.Clear();


 }

Use this method on button click event or on Page load event in aspx.cs page as follows:

protected void btnGenerateBarCode_onclick(object sender, EventArgs e) 

 {


 string barcodeDetail =”Vijendra”; 

 GenerateBarCode.CreateBarCode(barcodeDetail, "Free 3 of 9 Extended", 48,  "Your Physical path location" );

 imgBarcodeImage.ImageUrl = string.Format("BarcodeImage/{0}.png", barcodeDetail);


 }

Following is the .aspx code snippet for Barcode image.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="BarCode._Default" %>

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Sample of BarCode</title>

</head>

<body>

 <form id="form1" runat="server">

 <div>

 <asp:Button ID="btnGenerateBarCode" runat="server" onclick="btnGenerateBarCode_onclick" Text="Generate and Save Bar Code"/><br />  

 <br />

 <asp:Image ID="imgBarcodeImage" runat="server" />

     

 </div>

 </form>

</body>

</html> 

 

The ouput with this font will  be as follows:

Barcode for Free 3 of 9 Extended Font

this barcode is without the showing the text,if you want to show the text below the Barcode use IDAutomationHC39M insteade of Free 3 of 9 Extended font.

protected
void btnGenerateBarCode_onclick(object sender, EventArgs e) 

{


string barcodeDetail = “Vijendra”;

GenerateBarCode.CreateBarCode(barcodeDetail,"IDAutomationHC39M", 48,"Your Physical path location");


//Like D://MyProjects//BarCode//BarcodeImage//


imgBarcodeImage.ImageUrl = string.Format("BarcodeImage/{0}.png", barcodeDetail);

 
}

 

By this font the output will be as follows:

 

Barcode for IDAutomationHC39M

 

 Download a Sample Project

 

Hope it will help to all.  

 

Please Post your valuable comments.Cool

 



Comments (12)

   
hajan
Nice! I like the idea and the concept too.
6/15/2010
 · 
by
   
mony
mony
thnxxxxx
6/17/2010
 · 
by
   
vijjendra
Thanks Hajan and  mony.
6/17/2010
 · 
by
   
BigHeart
BigHeart
I Have A process to print ID with Barcode i want to use IDAutomationHC39M with textbox when i enter the ID and click button it should print ID with Barcode in thermal printer .
9/8/2012
 · 
by
   
Vijjendra
Dear BigHeart,           Barcode is just Image you can first create a barcode in image like above code,then you can print that image via printer.
9/8/2012
 · 
by
   
anil
anil
I have one requirement generate QR COde like this<html><head runat="server"></head><body><asp:RadioButton ID="RadioButton1" runat="server" ForeColor="#006600" GroupName="coupon" Text="QR Code" />                                <br />                                <asp:RadioButton ID="RadioButton2" runat="server" ForeColor="#006600" GroupName="coupon" Text="Bar Code" />                                <br />                                <asp:RadioButton ID="RadioButton3" runat="server" ForeColor="#006600" GroupName="coupon" Text="Coupon Code" />                            <aspropDownList ID="DropDownList1" runat="server" Height="24px" style="color: #0000FF; background-color: #66CCFF" Width="147px">                                    <asp:ListItem>Select type of value</asp:ListItem>                                    <asp:ListItem>$</asp:ListItem>                                    <asp:ListItem>%</asp:ListItem>                                </aspropDownList> <asp:TextBox ID="TextBox1" runat="server" Height="24px" style="color: #0000FF" >               </asp:TextBox>                                <asp:Button ID="Button1" runat="server" BackColor="#FF99CC" BorderStyle="Double" ForeColor="#003300" Height="31px" Text="Generate" OnClick="Button1_Click" /><asp:Image ID="Image2" runat="server" Height="96px" Width="126px" /></body></html>3 Radio butons,1 DropDown list,1textBox,1 ImageI want to select perticular (ex:Bar,QR codeetc),Select Type of One dropdown list value,Enter Value in textBox AND I want to click on the Genarate button Generate QR code(OR)Bar code image display in the button below Image control After scan that Image display total values what i am selecting in the above,Plz help me
11/26/2012
 · 
by
   
gurumoorthi
gurumoorthi
Its very usefull for me.. So thank tou very much...
2/26/2013
 · 
by
   
imageat
imageat
i have never generate barcode with asp.net before. all i can do is use the barcode generator in C#.net. here is what i will do with my sample codes: http://www.businessrefinery.com/barcode-generate/csharp_barcode_generator.html
5/22/2014
 · 
by
   
Vijjendra
Please see the attached Sample projects. you can also test the your generated barcode online http://www.onlinebarcodereader.com/
5/22/2014
 · 
by
   
andy
A few days ago,I generated a barcode in ASP.NET by myself with KA.Barcode for ASP.NET.I'd like to share my experience witn you guys.And do you have any good ideas?I want to try an easier way,thanks.
9/25/2014
 · 
by
   
ashhad saud
ashhad saud
I am getting error while i used GenerateBarCode. the error is The GenerateBarCode name does not exist. will you miising the library. Can you tell me if there is any library to use GenerateBarCode
2/12/2016
 · 
by
   
Vijjendra
Asshad GenerateBarCode is the name of the class which have the CreateBarcode Method. you can use your class name where you placed the CreateBarcode method.
5/23/2016
 · 
by
   
santhosh thomas
santhosh thomas
super. My barcode scanner was not scanning it. Then I found the 2 character (2 stars at beginning and end) width need to extend while generating code. modified as below: SizeF sizeF = graphics.MeasureString("*" + barCodedDetail+"*" , barCodeFont); thank you
6/23/2016
 · 
by
  • :*
  • :*
  • :
 *

Top Posts