Home > C# > Barcode generate using c#

Barcode generate 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()
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);
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);
pbBarcodeImage.Image = new Bitmap(path);
{ }

Categories: C#
  1. emily
    06/12/2013 at 3:34 PM

    You can also generate barcode image (BMP, EMF, GIF, JPEG, PNG, TIFF and WMF) in c# by using Aspose.Barcode for .NET Library. It has been very helpful for me, i hope you will like it also.


  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: