C# Barcode Reader in ASP.NET framework web form app

How to scan, read barcode images for ASP.NET framework web form applications with free C# barcode example source code

Using Free C# Souce Code to scan barcodes in ASP.NET web form framework Application & IIS Projects



This C# tutorial provides a step by step guide for building an ASP.NET framework web form application, where user can upload a image with barcode and scan, read the barcode data message from it. After this tutorial, you will be albe to

  • Read 20+ barcodes data from images in ASP.NET framework Razor pages, MVC, Blazor web application
  • Support .NET 9, 8, 7, 6, 5 and .NET Core 3.1, 2.1 ASP.NET Core web application

How to read linear, 2d barcodes in ASP.NET web form application using C#

  1. Download C#.NET Barcode Reader Library
  2. Install C# library to scan barcode images in ASP.NET web form
  3. Step by Step Tutorial












Prerequisites

Top
Download and install the following software on your computer
  1. Visual Studio 2022
  2. .NET Framework 4
  3. OnBarcode.Barcode.BarcodeScanner.dll (For .NET Framework 4.0)

Create an ASP.NET web form (Framework) application with barcode reader on web page using C#

Top

1. Create new ASP.NET web form project



Start Visual Studio 2022 and select "Create a new project".

Select "ASP.NET Web Application (.NET Framework)" in the dialog, and then press "Next" button.





Create a new web project with name "OnBarcodeReaderASPNETWebDemo".





Select Empty project template, and then press "Create"

Now, all auto-generated files for ASP.NET framework barcode reader demo web project could be found in the solution explorer.





2. Install Barcode Reader Library dll



Add OnBarcode Barcode Reader library for .NET Framework 4 reference "OnBarcode.Barcode.BarcodeScanner.dll".





3. Add web page to the project



Open "Add New Item" dialog by press [Ctrl+Shift+A]; and add a new item "Index.aspx" to the project.



Copy ALL following codes to replace the content of the file "Index.aspx".


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="OnBarcodeReaderASPNETWebDemo.Index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>OnBarcode Reader ASP.NET Web Demo</title>
</head>
<body>
    <form id="form1" runat="server" enctype="multipart/form-data">
        <div style="text-align:center">
            <br /><br />
            <asp:FileUpload ID="FileUpload1" runat="server" />
            &nbsp;&nbsp;
            Barcode Type:
            &nbsp;
            <asp:DropDownList ID="dropDownListBarcodeType" runat="server" AutoPostBack="true" />
            <br /><br />
            <asp:Button ID="UploadToScan" runat="server" Text="Scan Image" OnClick="UploadToScan_Click" />
            <br /><br />
            Scan Result:
            <br />
            <asp:TextBox ID="ScanResult" runat="server" Text="" Width="500" Height="100" TextMode="MultiLine" ReadOnly="true" />
        </div>
    </form>
</body>
</html>


Copy ALL following codes to replace the content in the file "Index.aspx.cs".


using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using OnBarcode.Barcode.BarcodeScanner;

namespace OnBarcodeReaderASPNETWebDemo
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                String[] types = new String[] {
                    "AustraliaPost", "Codabar", "Code39", "Code39Extension", "Code93",
                    "Code128", "DataMatrix", "EAN8", "EAN13", "Identcode",
                    "IntelligentMail", "Interleaved2of5", "ISBN", "ISSN", "ITF14",
                    "Leitcode", "PatchCode", "PDF417", "Planet", "Postnet",
                    "QRCode", "RM4SCC", "UPCA", "UPCE"            
                };
                this.dropDownListBarcodeType.DataSource = types;
                this.dropDownListBarcodeType.DataBind();
                //  Default Barcode Type: Code 128
                this.dropDownListBarcodeType.SelectedIndex = 5;
            }
        }

        protected void UploadToScan_Click(object sender, EventArgs e)
        {
            if (!this.FileUpload1.HasFile)
                return;

            try
            {
                String filename = Path.GetFileName(this.FileUpload1.FileName);

                StringBuilder sb = new StringBuilder();
                using (MemoryStream ms = new MemoryStream(this.FileUpload1.FileBytes))
                {
                    //  Get enum BarcodeType from ComboBox selected index.
                    BarcodeType barcodeType = (BarcodeType)this.dropDownListBarcodeType.SelectedIndex;

                    String[] result = BarcodeScanner.Scan(ms, barcodeType);
                    if (result.Length > 0)
                    {
                        sb.Append("Number of Barcode: " + result.Length + "\r\n");
                        foreach (String s in result)
                            sb.Append("Message: '" + s + "'\r\n");
                    }
                    else
                    {
                        sb.Append("No Barcode Found.");
                    }
                }

                this.ScanResult.Text = sb.ToString();
            }
            catch (Exception ex)
            {
                this.ScanResult.Text = "Error: " + ex.Message;
            }
        }
    }
}


4. Set Web.Config



Set start page to "Index.aspx" in Web.Config.

Copy contents in red to the Web.config file.


<?xml version="1.0" encoding="utf-8"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  https://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
    <system.web>
      <compilation debug="true" targetFramework="4.0" />
    </system.web>


    <system.webServer>
      <defaultDocument>
        <files>
          <clear />
           <add value="Index.aspx" />
        </files>
      </defaultDocument>
    </system.webServer>


</configuration>


5. Run barcode reader for ASP.NET Framework web form app



It is done. Now press "Ctrl+F5" to run the web project.





Use "Choose File" button to browse and add an image file with barcode(s).

Then, press "Scan Image" button to submit the form to server; and then the scan result would be shown in the scan result textbox as below.





















OnBarcode is a market-leading provider of barcode imaging generator, reader controls and components for ASP.NET, Windows Forms, WPF, as well Java, Android, iOS (iPhone, iPad) across all major enterprise development platforms. We provides comprehensive tutorials and how-tos for various linear, 2d barcode information, such as C# in ASP.NET, C# .NET, C# Barcode Encoding, C# Barcode Image, VB.NET in ASP.NET, VB.NET Winforms, VB.NET Barcode Encoding. OnBarcode barcode products are supported by RasterEdge ASP.NET Document Viewer, which supports ASP.NET PDF Viewer, ASP.NET MVC Document Viewer, ASP.NET PDF Editor, MVC PDF Viewer. And provide high quality C# Convert PDF to Tiff, C# Convert PDF to Word, C# Convert PDF to HTML, C# Convert PDF to Jpeg images, and their easy and simple documents, like C# PDF SDK, C# extract text from PDF, C# Compress PDF, Print PDF in C# and C# extract image from PDF.
Terms of Use | Privacy Policy
Copyright © OnBarcode.com . All rights reserved.