Barcode Generation using C#

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

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.

Sample Code

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
{ }
}

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

...S.VinothkumaR.

3 comments:

escort said...

Awesome blog Adam!! deneme linki I saw you all in Buffalo and the show was incredible, antalya pansiyonları the best I've ever seen and I can't wait to see the show again in Rochester! It's so great to hear you're having a good time. eskort met you in Cleveland and you seemed so escort kızlar ankara If things go my way, I'll be able to catch up with chat sitesi toptan mallar satış sitesi toptan satış deneme travesti istanbul
derteg deneme escort bayanlar istanbul If you don't know how to spot the difference, just watch any of the Filipinas buying and you'll soon catch on. izmir jigolo Crab-sexers par excellence, it seems there's nothing these people aren't good at! cinema izle cs

SteveBrad said...

This blog is provide lots of good idea and your blog are very usefull for me.Thanks a lot.
Buy Generic Viagra

katherine johanson said...

There is another .NET Component for Barcode available known as Aspose.Barcode for .NET, it allows developers to quickly and easily add barcode generation and recognition functionality to their .NET applications (WinForms, ASP.NET, WPF, .NET Compact Framework and Silverlight ). Try it, i hope you will find it useful also.