C# Barcode Generator for ASP.NET Core
How to create barcodes using c# in asp.net Core mvc web application

Generate, create, print linear, 2d barcode image labels in c# asp.net, windows applications.
Barcode for .NET Suite > Generate Barcode in C#





  • Generate QR Code, Data Matrix, PDF417 2d barcodes in C# application, also create Code 39, Code 128, EAN-13, UPC-A 20+ linear barcodes in C# .NET projects
  • Compatible with GS1 System, and support Unicode encoding in 2d barcodes
  • Easy to enable barcode generation in your ASP.NET Core, framework, Razor pages, MVC web applications
  • Support both System.Drawing.Common for Windows development and SkiaSharp for Linux, MacOS, non-Windows development
  • Fully support .NET 8, 7, 6, 5, .NET Core 3.x, 2.x ASP.NET Core web apps and .NET Framework 4.x, 3,x, 2.x ASP.NET web applications
  • Reliable C# barcode generator component to print high resolution dpi barcode for all scanners and printers

Tutorial: How to generate barcodes in ASP.NET Core web app using C#?

Step 1: Create a new ASP.NET Core web app project



1.1: Open Visual Studio 2022, create a new project.





1.2: Choose project type "ASP.NET Core Web App" with C# programming language





1.3: Change project name as "KeepAutomation.BarcodeGenerator.ASPNETCoreWebAppDemo". Click button "Next".





1.4: Choose .net Framework as ".NET 6.0 (Long Term Support)". Click button "Create"





Step 2: Add barcode generator library dll and Nuget package



2.1: In project Solution Explorer, right click "Dependencies". Click "Add Project Reference..." on the popup menu.





2.2: In the "Reference Manager" Window, click button "Browse..." to select the KeepAutomation barcode generator dll library.





2.3: In the popup Windows, choose the dll KeepAutomation.Barcode.Standard.dll from downloaded package /DLL/NetStandard/. If you have not downloaded the free trial package, you can dowload it here: KeepAutomation .NET Barcode Generator library





2.4: Now you can find that the .NET Barcode Generator dll has been installed on your ASP.NET Core web application successfully.





2.5: Now we will add a NuGet package "System.Drawing.Common". Right click "Dependencies" on Solution Explorer, and choose "Manage NuGet Packages..." from popup menu.





2.6: Go to Browse tab, and do search "System.Drawing.Common". Choose the right searched result, and click button "Install".





2.7: Here you have installed the NuGet package successfully on your ASP.NET web app.





Step 3: Add C# source code to generate barcodes on the web pages



3.1: In the Visual Studio, open file "Index.cshtml", and replace the content with the following C# codes.

@page
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

<div class="text-center">
    <form asp-page="./Index" method="post">
        <br />
        Barcode Message:
        <input type="text" id="tb1" name="tbMessage" style="width: 600px; " asp-for="@Model.Message" />
        <br />
        <br />
        <input type="submit" value="Create Barcode" />
    </form>
    <br />
    <img src="data:image/png;base64,@Model.BarcodeImage" />
</div>




3.2: Replace the file "Index.cshtml.cs" content with the following C# codes.

using KeepAutomation.Barcode.Bean;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Drawing.Imaging;

namespace KeepAutomation.BarcodeGenerator.ASPNETCoreWebAppDemo.Pages
{
    public class IndexModel : PageModel
    {
        public String Message { get; set; } = "Code 128";
        public String BarcodeImage { get; set; } = "";

        public Task<IActionResult> OnGet()
        {

            BarCode barcode = new BarCode();

            barcode.Symbology = Barcode.Symbology.Code128Auto;
            barcode.CodeToEncode = this.Message;
            barcode.ImageFormat = ImageFormat.Png;

            byte[] barcodeInBytes = barcode.generateBarcodeToByteArray();
            BarcodeImage = System.Convert.ToBase64String(barcodeInBytes);

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

        public Task<IActionResult> OnPost()
        {
            this.Message = Request.Form["tbMessage"].ToString();

            BarCode barcode = new BarCode();

            barcode.Symbology = Barcode.Symbology.Code128Auto;
            barcode.CodeToEncode = this.Message;
            barcode.ImageFormat = ImageFormat.Png;
            
            byte[] barcodeInBytes = barcode.generateBarcodeToByteArray();
            BarcodeImage = System.Convert.ToBase64String(barcodeInBytes);

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






Step 4: Run the ASP.NET Core web app to create barcodes online in the web browser



Now you can run the ASP.NET Core web project from Visual Studio, and generate barcodes online in the web browser





Tutorial: How to create barcodes using URL in ASP.NET Core web app using C#?

Step 1: Create a new ASP.NET Core web app with project template "ASP.NET Core Web App", or "ASP.NET Core Empty"

Step 2: Add .NET Barcode Generator dll to ASP.NET web project

Go to downloaded KeepAutomation .NET Barcode Generator package, navigate to /DLL/NetStandard/

Add the following two dlls to your ASP.NET Core web app project
  • KeepAutomation.Barcode.AspNetCore.dll
  • KeepAutomation.Barcode.Standard.dll
Step 3: Add NuGet package

In Visual Studio, Add NuGet package "System.Drawing.Common" package to your ASP.NET project. Step 4: Add C# source code to ASP.NET Program.cs file

Open file "Program.cs" in Visual Studio, and replace with the following C# source code.

using KeepAutomation.Barcode.Bean;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHttpContextAccessor();
var app = builder.Build();
// Linear
app.MapGet("/", async (IHttpContextAccessor httpContextAccessor) =>
{
    
    var context = httpContextAccessor.HttpContext;
    if (context != null)
        await KeepAutomation.Barcode.AspNetCore.BarCodeStream.generateBarcodeAsync(context.Request, context.Response);
});

app.Run();


Step 4: Run web app from the Visual Studio. The opened web browser will automatically display the rendered barcode (default is a Code 128 barcode) in the web browser.

You can use the url properties to change the barcode types, and barcode properties.