Barcode Generation using C#

No.of Views2032
Bookmarked0 times
Downloads 
Votes0
By  vinothnat   On  15 Feb 2010 22:02:38
Tag : CSharp , Windows Forms
Barcode Generation using C#
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this article, I am trying to give you a sample code for generating barcode image by using C#.

Prerequisite

1 .NET Framework 2.0

2.Microsoft Visual Studio 2005 or later


Description

We can see in market for all products they are using barcode only. So the barcode is common one. When we try to programming with old language for barcode is very difficult. We needed to go third party component. But in .Net framework it is very easy to generate barcode image.

Barcode font

There is lot of free barcode fonts available in internet. We can search and download it for generating the barcode image. Here is the font which is I am using "Barcodefont.ttf". We need to download this font and install in your fonts folder which is located in control panel.

For generating barcode image, we need to add the namespaces "System.Drawing, System.Drawing.Imaging" and the file management namespaces.


Code Snippets

{codecitation class="brush: csharp; gutter: true;" width="650px"}

using System.Drawing.Text;

using System.Drawing;

using System.IO;

using System.Drawing.Imaging;

public string dirPath = "";

dirPath = Environment.CurrentDirectory;

private void GenerateBarCode()

{

try

{

string path = dirPath + "\\" + DateTime.Now.ToFileTimeUtc().ToString() + ".jpg";

PrivateFontCollection fontCollection = new PrivateFontCollection();

fontCollection.AddFontFile(dirPath + "\\BarcodeFont.ttf");

FontFamily fontFamily = new FontFamily("barcode font", fontCollection);

Font font = new Font(fontFamily, 40);

Bitmap bmp = new Bitmap(pbBarcodeImage.Width, pbBarcodeImage.Height);

Graphics graphics = Graphics.FromImage(bmp);

graphics.Clear(Color.White);

SizeF sizeF = graphics.MeasureString(txtEnter.Text.Trim(), font);

Brush brush = new SolidBrush(Color.Black);

graphics.DrawString(txtEnter.Text.Trim(), font, brush, 10, 10);

bmp.Save(path, ImageFormat.Jpeg);

bmp.Dispose();

pbBarcodeImage.Image = new Bitmap(path);

}

catch

{ }

}

{/codecitation}

Conclusion

Hence we are generating the barcode by using the above code.

 
Sign Up to vote for this article
 
About Author
 
vinothnat
Occupation-Not Provided
Company-Not Provided
Member Type-Junior
Location-Not Provided
Joined date-03 Jun 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top