Saturday, January 5, 2008

How to Generate Barcode in ASP.NET

I will show you how to generate bar code images using .NET Built-in classes.
You will not need a Third Party tool or external DLL library.
As we all know that Barcodes depends on Fonts, there are plenty of them used for Barcodes.
The most famous font for Barcode is "Code 39" and anyone can download it for free.
After you download the font and install it to Windows Fonts, use the following method to generate Barcode images:

public void GenerateBarcodes(string productCode, string productName, string productPrice)
    Font stringFornt = new Font("Arial", 15, FontStyle.Bold, GraphicsUnit.Point);
    Font barcodeFont = new Font("Free 3 of 9", 40, FontStyle.Regular, GraphicsUnit.Point);
    Bitmap barcodeImage = new Bitmap(140, 110);
    Graphics graph = Graphics.FromImage(barcodeImage);

    graph = Graphics.FromImage(barcodeImage);
    graph.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
    graph.DrawString(productName, stringFornt, new SolidBrush(Color.Black), 10, 0);
    graph.DrawString(productCode, barcodeFont, new SolidBrush(Color.Black), 0, 30);
    graph.DrawString(productCode, stringFornt, new SolidBrush(Color.Black), 10, 70);
    graph.DrawString("#CODE#" + productPrice, stringFornt, new SolidBrush(Color.Black), 10, 90);

    barcodeImage.Save(Server.MapPath("~/BarcodeImages/barcode.jpg"), ImageFormat.Jpeg);



Hire Me

Follow me on Facebook

Follow me

Do you find this Blog helpful?

Follow by Email

About Me

My Photo
Expert Senior Software Developer

Microsoft Business Card