Pages

Saturday 4 August 2012

How to generate Barcode in Asp.Net?(Example-2)

I have already discussed about generating Barcode in my previous Post(http://mydotnetcollections.blogspot.in/2012/08/how-to-generate-barcode-in.html). Here, I am showing another example of generating Barcode.
    Here I have used another type of Font("Free 3 of 9"). You have to have this font installed in your system before using this example. You can download it from the internet and install it in your system.
   In this example, Barcodes of those strings which are created randomly are generated.
    First, add a .aspx page into your solution. Then write a method to generate random numbers(Already discussed in my previous Post "http://mydotnetcollections.blogspot.in/2012/08/how-to-generate-random-numbers-in-aspnet.html".  

    private readonly Random _rng = new Random();
    private const string _chars = "0123456789";
    private string RandomString(int size)
    {
        char[] buffer = new char[size];

        for (int i = 0; i < size; i++)
        {
            buffer[i] = _chars[_rng.Next(_chars.Length)];
        }
        return new string(buffer);
    }
      Then write another method which will take a string as parameter and will generate barcode of that string and will return the barcode as a Bitmap.
    private Bitmap CreateBarcode(string data)
    {
        Bitmap barCode = new Bitmap(1, 1);
 Font threeOfNine = new Font("Free 3 of 9", 15, System.Drawing.FontStyle.Regular,     
                    System.Drawing.GraphicsUnit.Point);
        Graphics graphics = Graphics.FromImage(barCode);
        SizeF dataSize = graphics.MeasureString(data, threeOfNine);
        barCode = new Bitmap(barCode, dataSize.ToSize());
        graphics = Graphics.FromImage(barCode);
        graphics.Clear(Color.White);
        graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
        graphics.DrawString(data, threeOfNine, new SolidBrush(Color.Black), 0, 0);
        graphics.Flush();
        threeOfNine.Dispose();
        graphics.Dispose();
        return barCode;
    }
Finally, write these following lines wherever you want to generate Barcode.
Response.ContentType = "image/gif";       
Response.Clear();
string barVal = RandomString(20);//I have taken 20 as my string's length
Bitmap barCode = CreateBarcode("*"+barVal+"*");
barCode.Save(Response.OutputStream, ImageFormat.Gif);
Response.End();
barCode.Dispose();
       In my example, I have generated Barcodes everytime  I refresh the Page. That's why I have placed the above lines of Code inside my Page_Load Event.
protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "image/gif";
        Response.Clear();
        string barVal = RandomString(20);
        Bitmap barCode = CreateBarcode("*"+barVal+"*");
        barCode.Save(Response.OutputStream, ImageFormat.Gif);
        Response.End();
        barCode.Dispose();
    }

Final Output:
     Here, I am showing only five Barcodes generated randomly.


Regards,
Prajanuranjan....




No comments:

Post a Comment

Total Pageviews