How to scan barcode in asp.net web application?

back to home  >  Tech FAQ  >  Scan barcode in asp.net web application using C#

Question:

Hi,there, I'm asked to make as asp.net project with simple functions. It can allow users to upload barcode images(bmp, jpg, png, gif or tiff file), after that, users can decode the barcodes out, and show the results to the html page. Can you give us some recommendation using C# code?

---- Quincy

Answer:

Hi, Quincy.

To make things simple, you only need add two web controls into the aspx page. One is FileUpload control, the other is Button.

You can use the FileUpload control to upload the barcode image files in the asp.net application. In addition, you can insert a Image web control to the aspx page, after the barcode image uploaded, make this web Image control to display the barcode image dynamically. Please note, if you want to show this barcode image instant by ajax technique, you need use the UpdatePanel provided by Microsoft. Just insert the web Image into the UpdatePanel, the Image control can refresh immediately while the upload barcode image changed.

Then while you clicking the button, you can define the customized click event in the aspx.cs. Such as reading and scanning barcode feature can be added in this server side event.

Here we post the main sample C# code in the asp.net web page. You can make your own web application just like this. Please add the "PQScan.BarcodeScanner.dll" to your project reference.

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PQScan.BarcodeScanner;

namespace BarcodeReaderWebDemo
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        { 
        }

        protected void btUpload_Click(object sender, EventArgs e)
        {
            string name = this.FileUpload1.FileName;
            string path = Server.MapPath("UploadFiles") + "\\" + name;

            if (System.IO.File.Exists(path))
            {
                System.IO.File.Delete(path);
            }

            FileUpload1.SaveAs(path);

            Session["filepath"] = path;
        }

        protected void btRead_Click(object sender, EventArgs e)
        {
            string result = "";

            string path = Session["filepath"].ToString();

            BarcodeResult[] barResults = BarCodeScanner.Scan(path); 

            if (barResults != null)
            {
                result = "";
                for (inti = 0; i<barResults.Length; i++)
                {
                    result += barResults[i].BarType.ToString() + " -- " + barResults[i].Data + "\r\n";
                }
            }

            this.TextBox1.Text = result;   
        }
    }
}

Hope the C# code can be helpful.

---- pqScan Support Team

Online Demo Download Trial Can I Read Barcode in PDF document using C#? Need help for reading barcode with unconventional position? How to read barcode from Microsoft Office? Can I read barcode image from report in C# .NET? Can I read color barcode image in C# .NET? Is it available for reading all of the barcode in a picture by one time? How to scan barcode from webcam or other device? Can I use your barcode reading SDK in Mono? I can't scan barcode from my images with different DPI. How to scan barcode in asp.net web application? How to scan barcode in WPF project using C# language? Can I run your .net barcode reader library in Windows7 or Windows8? How to scan rotated barcode in .net project?