C# Barcode Reader Library in ASP.NET Core web app

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

Using Free C# Souce Code to Generate Barcode Labels for ASP.NET Web Application & IIS Projects



This C# tutorial provides a step by step guide for building an ASP.NET Core web app, 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 Core Razor pages, MVC, Blazor web application
  • Support both System.Drawing.Common for Windows and SkiaSharp for Linux, MacOS
  • Support .NET 7, 6, 5 and .NET Core 3.1, 2.1 ASP.NET Core web application

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

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












Prerequisites

Top
Download and install the following software on your computer
  1. Visual Studio 2022
  2. .NET 6.0 SDK
  3. OnBarcode.Barcode.BarcodeScanner.Skia.dll

Create an ASP.NET Core web app with barcode reader on web page using C#

Top

1. Create new ASP.NET Core web app



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





Select "ASP.NET Core Web App" in the dialog, and then press "Next" button.





Create a new web project with name "OnBarcode.BarcodeReader.ASPNETCore.Demo".





Select .NET 6.0 (Long-term support), and then press "Create"





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





Open the file "Program.cs" and make sure that it has enabled following Middlewares.

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapRazorPages();

app.Run();


2. Install SkiaSharp and OnBarcode Barcode Reader for ASP.NET Core dll



Right-click "Dependencies" in the Solution Explorer, and select "Manage NuGet Packages".





Select "Browse" and use the search control to find "SkiaSharp" from the package source "nugget.org".





Choose the latest stable version to install SkiaSharp.





Check "Packages" in the Solution Explorer to ensure the installation is success.





Add OnBarcode Barcode Reader library for ASP.NET Core DLL reference "OnBarcode.Barcode.BarcodeScanner.Skia.dll".





Add C# code to scan, read barcodes in ASP.NET Core web page



Copy all following codes to replace the content of the file "Index.cshtml".
@page
@model IndexModel
@{
    ViewData["Title"] = "Scan Barcode";
}

<div class="text-center">
    <form enctype="multipart/form-data" method="post">    
        <input asp-for="@Model.FormFile" type="file" style="width:300px; "
               onchange="document.getElementById('Submit').removeAttribute('disabled')" />
        &nbsp;&nbsp;
        Barcode Type:
        <select name="BarcodeType">
            @for (var i = 0; i < Model.BarcodeTypes.Length; i++)
            {
                @if (i == @Model.SelectedType)
                {
                    <option value="@i" selected>@Model.BarcodeTypes[i]</option>
                }
                else
                {
                    <option value="@i">@Model.BarcodeTypes[i]</option>
                }
            }
        </select>
        <br /><br />
        <input id="Submit" type="submit" value="Scan Image" disabled />
    </form>
    <br />
    <table name="ScanResult" class="table-bordered" style="width:40%;" align="center">
        <thead>
            <tr>
                <td colspan="2">Scan Result</td>
            </tr>            
        </thead>
        <tbody>
            @if (Model.ResultCount >= 0)
            {
                <tr>
                    <td colspan="2" align="left">
                        Number of valid barcodes:&nbsp;&nbsp;@Model.ResultCount
                    </td>
                </tr>
            }
            @for (var i = 0; i < Model.ResultCount; i++)
            {
                var idx = i + 1;
                var msg = Model.Results[i];
                <tr>
                    <td style="width:10%;">@idx:</td>
                    <td align="left">'@msg'</td>
                </tr>
            }
        </tbody>
    </table>
</div>


Copy all following codes to replace the content in the file "Index.cshtml.cs".
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using OnBarcode.Barcode.BarcodeScanner;

namespace OnBarcode.BarcodeReader.ASPNETCore.Demo.Pages
{
    public class IndexModel : PageModel
    {
        public int SelectedType { get; set; } = 5;
        public String[] BarcodeTypes { get; set; } = 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"
        };
        public IFormFile? FormFile { get; set; }
        public int ResultCount { get; set; } = -1;
        public String[] Results { get; set; } = new String[0];

        public void OnGet()
        {
        }

        public Task<IActionResult> OnPost()
        {
            this.SelectedType = Int32.Parse(this.Request.Form["BarcodeType"]);

            if (this.FormFile != null)
            {
                var file = this.Request.Form.Files[0];
                using (MemoryStream ms = new MemoryStream())
                {
                    file.CopyTo(ms);

                    String[] result = BarcodeScanner.Scan(ms, (BarcodeType)this.SelectedType);
                    if (result != null)
                    {
                        this.ResultCount = result.Length;
                        this.Results = result;
                    }
                    else
                        this.ResultCount = 0;
                }
            }

            return Task.FromResult<IActionResult>(Page());
        }
    }
}


4. Run barcode reader for ASP.NET Core web app



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



It launches the Kestrel server and opens the default browser. 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 all barcodes found in the image would be shown in a table 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.