Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Create BarCode Images in ASP.NET

0.00/5 (No votes)
9 Feb 2014 1  
Generate barcode image in web application

Introduction 

I am presenting this nice tip which will be useful for all those who are developing web based barcode systems, as I was one of them and faced a lot of problem as I could not find the right tech for developing client side barcode image using server side scripting.

Background

Before reading this tip, you need to have basic knowledge of Windows application, and web application using .NET. I used backend code as C#.NET and integrate the CODE39 barcode, which can be very useful for developers who are using barcode based application in web and also get many ideas for implementing desktop ideas in web applications.

Using the Code

I am also attaching the complete source code along with this so that you can edit or make changes according to your requirements. In this, firstly I created a Windows Application through which I generate bar codes and save the images in folder, and the generated application will be added and used in web application by using the namespace System.Diagnostics library.

Now here is the idea, how it works.

Firstly, we get an alphanumeric code combination which is converted in barcode image and thus, I take textbox to enter the code, i.e., you can generate the code according to your requirement, and then save the code in file using .NET library as System.IO.

if (File.Exists(Server.MapPath("Barcode.txt")))
        {
            File.Delete(Server.MapPath("BarCode.txt"));    
        }
        //File.Create(Server.MapPath("BarCode.txt"));
        File.WriteAllText(Server.MapPath("BarCode.txt"), TextBox1.Text);		  

After saving the code in text file, we will call the .exe file which is already included in our application which accesses the text file on page_load event and generates barcode and is saved in folder.

For calling the .exe files in web application, use the below code:

Process.Start(Server.MapPath("BarCodeGenerate.exe"));		   

Once the barcode is saved in images folder, then the application will be closed.

Show Barcode image on web page, then write the line:

Image1.ImageUrl = "images/" + TextBox1.Text + ".png"; 

I am adding the sample project and Windows application with this which might give you a better understanding of this.

In Windows application, I use 3 classes for generating barcode, named as:

  • BarcodeLib.cs
  • IBarCode.cs
  • Code39.cs

Now here is the application understanding which is used for creating bar codes, firstly create classes as follows:

public void createbarcode()
        {
            Bitmap temp = new Bitmap(1, 1);
            temp.SetPixel(0, 0, this.BackColor);
            pictureBox1.Image = (Image)temp;


            int W = Convert.ToInt32(200);
            int H = Convert.ToInt32(50);

            BarcodeLib.TYPE type = BarcodeLib.TYPE.UNSPECIFIED;
            type = BarcodeLib.TYPE.CODE39;

            try
            {
                if (type != BarcodeLib.TYPE.UNSPECIFIED)
                {
                    //To generate label
                    b.IncludeLabel = true;

                    //===== Encoding performed here =====
                    pictureBox1.Image = b.Encode(type, dt.Trim(), this.b.ForeColor, this.b.BackColor, W, H);
                    //===================================
                }

                pictureBox1.Width = pictureBox1.Image.Width;
                pictureBox1.Height = pictureBox1.Image.Height;

                pictureBox1.Image.Save(imgsavepath + barcodeimage);

                Application.Exit();
            }
            catch (Exception ex)
            {
                Application.Exit();
            }
        } 

Please include these files in your application to integrate this.

Points of Interest

I learnt to access desktop application in websites and get dynamic images and save them on server and can access those images on client end to read and print barcodes.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here