Click here to Skip to main content
15,849,829 members
Articles / Web Development / ASP.NET
Tip/Trick

Create BarCode Images in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.74/5 (16 votes)
9 Feb 2014CPOL2 min read 62.9K   28   8
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.

C++
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:

C++
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:

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

Image 1

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:

C++
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Global Infinite Technologies Pvt. Ltd.
India India
I'm a technology lover and when I say technology, I mean the latest, not the old school chalk board types. I love technology and those who know me, know that I'm a '24 hours - technology linked' guy. When i was in class 6th my dad surprised me with a PIII PC whose configuration was 533 MHz + 64 MB + 32 GB and I LOVED IT!!! Since then I started loving 'IT'. (Though, today my nokia lumia 925 has much higher configuration Wink | ;) )

some people are born to be an entrepreneur. They employ others rather being employed by others. I'm one of them. With the co-founder, Mr. Nadeem Lohani, I founded my company - Infinite Technologies. After three years of hard work and successfully developing more than 100 applications and websites, now my company is recognized as Global Infinite Technologies Pvt. Ltd. Today, I have a vibrant and enthausiastic team of engineers working towards the goal of the organization and we take care of their development.

I want to tell you that I spend my day reading technical articles and making hobbist projects related to computer technology. But, if you've already read the professional page, then trust me - I'm a fun loving person and don't miss any chance to go out with my friends whenever I get some time from computers.

Comments and Discussions

 
QuestionNeed .cs file for CODE128B Pin
Member 39001721-Jun-15 19:59
Member 39001721-Jun-15 19:59 
QuestionThanks. It is very nice article. Pin
Member 1134441417-Apr-15 2:23
Member 1134441417-Apr-15 2:23 
QuestionVery Nice Barcode Pin
tunly10-Dec-14 21:48
tunly10-Dec-14 21:48 
Questiontransparency issue Pin
amywhite9896-May-14 17:48
amywhite9896-May-14 17:48 
AnswerRe: transparency issue Pin
Er. Mayank Kothari24-Aug-14 7:36
professionalEr. Mayank Kothari24-Aug-14 7:36 
Question5 star from me, But :) Pin
hossein928-Apr-14 7:58
professionalhossein928-Apr-14 7:58 
GeneralMy vote of 4 Pin
AmitGajjar9-Feb-14 19:21
professionalAmitGajjar9-Feb-14 19:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.