Thursday 6 June 2013

Easy way to Generate Barcode in ASP.NET C#

1.First Download the Barcode.dll file below shown link 


2.Add .dll file Reference to your project 

3.Add this Namespace to cs file


using System.Drawing;
using System.Drawing.Imaging;

using BarcodeLib;
using System.Collections.Generic;
using System.IO;

4. Copy this Code in .cs file

 BarcodeLib.Barcode barcode = new BarcodeLib.Barcode()
                    {
                        IncludeLabel = false,
                        Alignment = AlignmentPositions.LEFT,
                        Width = 200,
                        Height = 30,
                        RotateFlipType = RotateFlipType.RotateNoneFlipNone,
                        BackColor = Color.White,
                        ForeColor = Color.Black,
                    };

                    System.Drawing.Image img = barcode.Encode(TYPE.CODE39, "0123456789");


                    string sGuid;
                    sGuid = Convert.ToString(Guid.NewGuid());
                    sGuid = sGuid.Replace("-", "");
                    img.Save(Server.MapPath("~/BarCodeImages/" + name + "/") + sGuid + ".jpg",     ImageFormat.Jpeg);
                   
                    image1.ImageUrl = "~/BarCodeImages/" + name + "/" + sGuid + ".jpg";

5.Source 

 <asp:Image ID="Image1" runat="server" />


Bar code is store in Barcodeimages folder ,add image control to source page ,generated bar code is shown in image control 

3 comments:

  1. You can also use C#/.NET Library for generating barcode like Aspose.Barcode for .NET, It has the ability to export to multiple image formats including: BMP, EMF, GIF, JPEG, PNG, TIFF and WMF. Below is the link of this Library if you want to try it:

    http://www.aspose.com/.net/barcode-component.aspx

    ReplyDelete
  2. Good article. I hope my text can be encoded as barcode in asp.net with c#. I try your method, it work perfectly. Thanks for sharing!

    ReplyDelete
  3. This method is really Easy way to Generate Barcode in ASP.NET C#, thank you bloggers for sharing.

    ReplyDelete