Our Customers
Contact Us Email: support@barcodelib.com
Home > .NET Barcode > How to barcode in C#? > How to generate barcode using C# in ASP.NET Core web app?
Download Barcode for .NET Suite Trial

How to create barcodes in C#
ASP.NET Core web app?

Detailed Developer Guide for Rendering Barcodes in ASP.NET Core web app Using C# Code
How to create barcode using C# in ASP.NET Core web app?

Requirements

In the following step by step tutorial, we'll show you how to quickly create a new ASP.NET Core web app, and integrate online barcode generating feature on ASP.NET web page using C# codes.

1. Create a new ASP.NET Core web app

Here we will quickly create a new ASP.NET Core web app using Visual Studio .NET
  • Start Visual Studio and select Create a new project
  • In the Create a new project dialog, select ASP.NET Core Web App, and select button Next.
  • In the Configure your new project dialog, enter BarcodeLib.Create.ASPNETCoreDemo for Project name. It is important to name the proejct BarcodeLib.Create.ASPNETCoreDemo, including letter case, so the namespaces will match when you copy, paste the following C# example code.
  • Select button Create
  • In the Additional information diaplog, select .NET 6.0 or .NET 7.0 for the Framework dropdowns. Select button Create.
  • Now we have created a new ASP.NET Core project successfully.

2. Install Barcode Generator library and Nuget package

Now we will add BarcodeLib barcode generator library to the asp.net project and also add a Nuget package named "System.Drawing.Common".

Add BarcodeLib Barcode Generator library

  • In the Solution Explorer window, right click Dependencies, select Add Project Reference...
  • Select button Browse... in the Reference Manager dialog.
  • Choose dll file BarcodeLib.Barcode.Common.dll from BarcodeLib downloaded package /Dll/net-standard/
  • Check BarcodeLib.Barcode.Common.dll is selected, and select button OK


  • Now we have added the barcodelib barcode generator library dll. We will start to add a library "System.Drawing.Common" from Nuget


  • Install NuGet Package

  • Right click Dependencies, select Manage NuGet Packages...
  • Navigate to tab Browse in the NuGet Package Manager
  • Enter System.Drawing.Common to search NuGet package
  • Choose the first searched result System.Drawing.Common by Microsoft
  • Select button Install


  • Now we have installed the barcode generator library and nuget package successfully.



For ASP.NET Core web app on Linux, MacOC ....

The above BarcodeLib barcode library and NuGet package is for ASP.NET web app running on Windows operating systems. If you will run ASP.NET web app on non-Windows operating systems, such as Linux, MacOS, you need install the following library dll and NuGet package.

  • BarcodeLib.Barcode.SkiaSharp.dll from BarcodeLib downloaded package /Dll/net-standard/
  • SkiaSharp by Microsoft from NuGet Package Manager


3. Add C# code to create barcode on a Razor Page

  • Copy the following code to file "Index.cshtml"
    @page
    @model IndexModel
    @{
        ViewData["Title"] = "BarcodeLib ASP.NET Core Barcode Generator";
    }
    
    <div class="text-center">
        <form asp-page="./Index" method="post">
            <br />
            Barcode data:  
            <input type="text" id="tb1" name="tbBarcodeData" style="width: 600px; " asp-for="@Model.BarcodeData" />
            <br />
            <br />
            <input type="submit" value="Create Barcode" />
        </form>
        <br />
        <img src="data:image/png;base64,@Model.BarcodeImage" />
    </div>
    
  • Copy the following C# code to file "Index.cshtml.cs"
    using BarcodeLib.Barcode;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.RazorPages;
    using System.Drawing.Imaging;
    
    namespace BarcodeLib.Create.ASPNETCoreDemo.Pages
    {
        public class IndexModel : PageModel
        {
            public String BarcodeData { get; set; } = "12345";
            public String BarcodeImage { get; set; } = "";
    
            public Task<IActionResult> OnGet()
            {
                byte[] dataBytes = getBarcodeInBytes();
    
                BarcodeImage = System.Convert.ToBase64String(dataBytes);
    
                return Task.FromResult<IActionResult>(Page());
            }
    
            public Task<IActionResult> OnPost()
            {
                this.BarcodeData = Request.Form["tbBarcodeData"].ToString();
    
                byte[] dataBytes = getBarcodeInBytes();
    
                BarcodeImage = System.Convert.ToBase64String(dataBytes);
    
                return Task.FromResult<IActionResult>(Page());
            }
    
            private byte[] getBarcodeInBytes()
            {
                BarcodeLib.Barcode.Linear barcode = new BarcodeLib.Barcode.Linear();
    
                barcode.Type = BarcodeLib.Barcode.BarcodeType.CODE128;
                barcode.Data = this.BarcodeData;
    
                barcode.ImageFormat = ImageFormat.Png;
    
                return barcode.drawBarcodeAsBytes();
            }
        }
    }
    


4. Run the ASP.NET web app

Now you have completed the demo app. Run the ASP.NET Core web app directly from Visual Studio. You can generate Code 128 barcodes online by entering Code 128 data online.



5. Customize the demo web app

You can easily customize the demo ASP.NET Core web app. For example, you can update the C# code inside method getBarcodeInBytes() to create QR Code online.

        private byte[] getBarcodeInBytes()
        {
            QRCode barcode = new QRCode();

            barcode.Data = this.BarcodeData;

            barcode.ResizeImage = true;
            barcode.ImageWidth = 400;
            barcode.ImageHeight = 400;
            barcode.ImageFormat = ImageFormat.Png;

            return barcode.drawBarcodeAsBytes();
        }


Related Barcode Library Tutorials
Barcode for Visual C#.NET - Other Supported Barcodes
Linear / 1D Barcodes:
Matrix / 2D Barcodes: