Scan Documents and Read Barcodes in ASP.NET

Last Updated on 2018-10-08

Document scanning and barcode reading are vital parts of almost any document workflows. Acquire documents from scanners, use barcodes to separate batch documents or group documents and then save the files to disk, database, or document library are typical steps people take to organize paper documents.

In this article, I will provide samples that show how to develop an ASP.NET application with TWAIN scanning and barcode reading by using Dynamic Web TWAIN and Dynamsoft Barcode Reader SDK.


Scan Documents and Recognize Barcodes in ASP.NET

You can read the barcodes on the client side or server side by using Dynamsoft imaging SDKs. Below we will show the code to read barcodes on the server side. If you want to do client-side barcode reading, please check How to Create a Online Barcode Scanner Application in JavaScript.

In Visual Studio, create a new web application.

Add Resources folder under the installation directory of Dynamic Web TWAIN, which includes the deployment files of the TWAIN SDK, to the project.

Add a web form page (Default.aspx) for document scanning. Embed Dynamic Web TWAIN control / plug-in to the page for displaying the scanned images. Check How to integrate Dynamic Web TWAIN to a web page

Add two buttons and a place holder, dwtcontrolContainer, for the Dynamic Web TWAIN object to the page.

    <select size="1" id="source" style="position: relative; width: 220px;"></select>
    <input type="button" value="Scan" onclick="AcquireImage();" />
    <input type="button" value="Load" onclick="LoadImage();" />

    <div id="dwtcontrolContainer"></div>
    <input type="button" value="Read Barcode" onclick="UploadImage();" />

Write JS code to use Dynamic Web TWAIN to scan documents from scanners.

function AcquireImage() {
    if (DWObject) {
        DWObject.IfDisableSourceAfterAcquire = true; // Scanner source will be disabled/closed automatically after the scan.

Use Dynamic Web TWAIN’s HTTP Post method to upload the scanned images in buffer to server side for barcode recognition.

    // OnHttpUploadSuccess is the callback function for successful uploads while OnHttpUploadFailure is for failed ones.
    function OnHttpUploadSuccess() {

    function OnHttpUploadFailure(errorCode, errorString, sHttpResponse) {

    function UploadImage() {
        if (DWObject) {
            // If no image in buffer, return the function
            if (DWObject.HowManyImagesInBuffer == 0)

            var strHTTPServer = location.hostname; //The name of the HTTP server. For example: "";
            var CurrentPathName = unescape(location.pathname);
            var CurrentPath = CurrentPathName.substring(0, CurrentPathName.lastIndexOf("/") + 1);
            var strActionPage = CurrentPath + "ReadBarcode.aspx";
            DWObject.IfSSL = false; // Set whether SSL is used
            DWObject.HTTPPort = location.port == "" ? 80 : location.port;

            var Digital = new Date(); 
            var uploadfilename = Digital.getTime() + Digital.getMilliseconds() + ".tif"; // Uses milliseconds according to local time as the file name

            //Upload image(s) to server side for barcode reading
            DWObject.SetHTTPFormField('left', ileft);
            DWObject.SetHTTPFormField('top', itop);
            DWObject.SetHTTPFormField('right', iright);
            DWObject.SetHTTPFormField('bottom', ibottom);
            DWObject.HTTPUploadThroughPostAsMultiPageTIFF(strHTTPServer, strActionPage, uploadfilename, OnHttpUploadSuccess, OnHttpUploadFailure);

Create ReadBarcode.aspx to receive the scanned images and recognize the barcodes on the images.

using Dynamsoft.Barcode;

namespace DBR_DWT_ASP.NET
    public partial class ReadBarcode : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
                /***********************Save Image on Server**********************/
                String strImageName;
                String strleft = HttpContext.Current.Request["left"];
                String strtop = HttpContext.Current.Request["top"];
                String strright = HttpContext.Current.Request["right"];
                String strbottom = HttpContext.Current.Request["bottom"];
                HttpFileCollection files = HttpContext.Current.Request.Files;
                HttpPostedFile uploadfile = files["RemoteFile"];
                strImageName = uploadfile.FileName;
                Rectangle rect;
                strImageName = Server.MapPath("/") + "\\UploadedImages\\" + uploadfile.FileName;

                /***********************Read Barcode Image**********************/
                BarcodeReader reader = new BarcodeReader();
                ReaderOptions options = new ReaderOptions();
                options.MaxBarcodesToReadPerPage = 100;
                //options.BarcodeFormats = (BarcodeFormat.CODE_39 | BarcodeFormat.CODE_128);
                options.BarcodeFormats = BarcodeFormat.OneD;

                reader.ReaderOptions = options;
                reader.LicenseKeys = "0925EFE71C10001015A17857C54DA148";

                BarcodeResult[] results = null;
                if (System.Int32.Parse(strbottom) > 0)
                    rect = new Rectangle(System.Int32.Parse(strleft), System.Int32.Parse(strtop), System.Int32.Parse(strright) - System.Int32.Parse(strleft), System.Int32.Parse(strbottom) - System.Int32.Parse(strtop));
                    results = reader.DecodeFileRect(strImageName, rect);
                    results = reader.DecodeFile(strImageName);

                strleft = ""; strtop = ""; strright = ""; strbottom = "";

                if (results == null)
                    Response.Write("No Barcode Detected");
                    return ;

                /**********************Return Barcode Rsults*********************/
                String strResults = "Total barcode(s) found: " + results.Length.ToString() + ".\n";
                for (int i = 0; i < results.Length; ++i)
                    BarcodeResult barcode = results[i];
                    strResults += "Barcode " + (i + 1).ToString() + ":\n";
                    strResults += barcode.BarcodeFormat.ToString() + "\n";
                    strResults += barcode.BarcodeText + "\n\n";



By following those steps, you will make a barcode scanning application easily. Let me know if you have any questions when running the sample code or creating your own document scanning application.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe Newsletter

Subscribe to our mailing list to get the monthly update.