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.Clear(Color.White);
    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);
    graph.Flush();

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

    barcodeImage.Dispose();
    graph.Dispose();
    barcodeFont.Dispose();
}

Thursday, January 3, 2008

How to Clear GridView Quickly

If you need to completely clear a GridView quickly, write the following code:

protected void ClearGrid(GridView gv)
{
    gv.DataSource = null;
    gv.DataSourceID = null;
    gv.DataBind();
}

Tuesday, January 1, 2008

Welcome to my Blog

Hello everyone,

I hope you find my blog interesting and useful for you. I will be adding a very good articles about everything related to Software Industry, New Technologies, Source Code, Fresh Ideas, Solutions to the Common Problems, and More...

You are welcome to send me any questions to amr.saafan@hotmail.com

Don't forget to leave comments for every article, your feedback is very important to me.
Please visit my Blog Frequently to be able to see new articles.

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