posted 5/4/2010 by Vijendra Shakya
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: 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//
By this font the output will be as follows:
Download a Sample Project
Hope it will help to all.
Please Post your valuable comments.
Thanks Hajan and mony.
Nice! I like the idea and the concept too.
thnxxxxx
What kind of email newsletter would you prefer to receive from CodeAsp.Net?18