Home > ASP.NET Barcode > ASP.NET Barcode Generation Guide - Using ASP.NET Barcode Library DLL for Barcode Image Generation

How to create barcode using ASP.NET Barcode Generator library?

Generate 1D and 2D Barcode Image for ASP.NET Web, ASP.NET Class and IIS Projects
  • Support complete integration and fast barcodes generation in ASP.NET Core web applications
  • Encode high print quality barcode images in JPEG, GIF or PNG format for ASP.NET web services
  • Fully build in managed C#, providing free C# & VB.NET sample codes
  • Supports .NET 11, 10, 9, 8, 7, 6, 5, .NET Core 3.x, 2.x, .NET Standard 2.0, and .NET Framework 4.x, 3.x, 2.x
  • Top-notch and competitive asp.net barcode component dll on the market
  • Compatible with latest 1D and 2D barcode symbology standards
  • Create linear barcodes for ASP.NET web, like Code39, Code128, GS1-128, Interleaved 2 of 5, EAN / UPC
  • Create 2d barcode images for ASP.NET web, like Data Matrix, PDF-417, & QR-Code
  • Print barcode in high resolution PNG, TIFF, Bitmap raster images and SVG, EPS vector images
  • Fulfill development needs by providing royalty-free developer license for ASP.NET Web Barcode Control DLL
This document is about how you can do with ASP.NET Barcode Generator component to generate barcodes in your ASP.NET class, ASPX design pages applications.

ASP.NET Barcode Generator Library

QR Code, Data Matrix, Code 128, GS1, EAN/UPC. Unicode text encoding in C# ASP.NET web application.

Download Now
NuGet Ready
.NET 5+
.NET Framework 4.x, 3.x, 2.x


Here we will learn how to create barcode images in ASP.NET web application.
  • Create barcode image in ASP.NET Core C# class
  • Print barcode image in ASP.NET Core Razor Page
  • Create barcode images using URL
  • Generate barcodes in ASP.NET Framework web application
BarcodeLib ASP.NET Barcode Generator library supports the following .NET versions
  • .NET 5+ (including .NET 11, 10, 9, 8, 7, 6, 5)
  • .NET Core 3.x, 2.x
  • .NET Standard 2.0
  • .NET Framework 4.x, 3.x, 2.x
ASP.NET Barcode Generator DLL - Supporting Symbologies
All Linear / 1D Barcodes:
All Matrix / 2D Barcodes:
ASP.NET Barcode Generates 2D (Matrix) Barcode Symbologies: Data Matrix, PDF417, QR Code.




Create barcode in ASP.NET Core C# class

Install

  1. Add the following dlls to your ASP.NET Core web app project reference.
    • BarcodeLib.Barcode.Common.dll
    • BarcodeLib.Barcode.AspNetCore.dll
  2. Install NuGet package System.Drawing.Common from NuGet Package Manager

Generate barcode image in ASP.NET C# code

The following free C#.net code illustrates how to generate a Code-128 barcode image in an ASP.NET C# class.
  • Create a Linear object for 1d barcode generation. To create 2d barcodes, you need choose QRCode, DataMatrix, PDF417.
  • Choose the 1d barcode type as BarcodeType.CODE128
  • Set the barcode encoding text string.
  • Call method drawBarcode() to print barcode to an image file.
Linear barcode = new Linear();
barcode.Type = BarcodeType.CODE128;
barcode.Data = "Code 128 Barcode";
barcode.drawBarcode("barcode image file path");






How to print barcode in Razor Page .cshtml img tag?

Using BarcodeLib ASP.NET Barcode Generator library, you can easily print and render barcodes in ASP.NET Razor Page (.cshtml) img tag.

Here we will create and print a Code 128 barcode in Razor Page Index.cshtml image tag.


Step 1. Create, customize barcode in ASP.NET C# class

  1. Open file Index.cshtml.cs in Visual Studio
  2. In class IndexModel.
    Define a String object BarcodeImage to store the barcode image content.

    public class IndexModel : PageModel
    {
           public String BarcodeImage { get; set; } = "";
    
  3. In method OnPost().
    • Create a Code 128 barcode. Print and store barcode image content to a byte array using method drawBarcodeAsBytes().
    • Use method ToBase64String() to convert barcode byte array data to a string object, and store the data in object BarcodeImage
    public Task<IActionResult> OnPost()
    {
        Linear barcode = new Linear();
        barcode.Type = BarcodeType.CODE128;
        barcode.Data = "Barcode data is here";
        BarcodeImage = System.Convert.ToBase64String(barcode.drawBarcodeAsBytes());
    
        return Task.FromResult<IActionResult>(Page());
    }
    


Step 2. Print and render barcode image in Razor page

In file Index.cshtml.
Insert the following img tag, where you want to show to barcode image.

<img src="data:image/png;base64,@Model.BarcodeImage" width=400px />


Now you can print and display a barcode in the ASP.NET Razor Page.



Customize, print barcode image size in Razor Page

You can specify the generated barcode image dimension size in Razor Page. And you can use web browser to print the web page with specified barcode image size.

Here we will generate barcode images that are 4 inches wide and 1.6 inches high.
  1. In file Index.cshtml.cs.
    Specify the barcode image unit of measure, image width and height in C# code.
    barcode.UOM = UnitOfMeasure.INCH;
    barcode.ImageWidth = 4;
    barcode.ImageHeight = 1.6f;
    barcode.Resolution = 900;
    
  2. In file Index.cshtml.
    Specify the customized barcode image size in html tag img.
    <img src="data:image/png;base64,@Model.BarcodeImage" style="width: 4in; height: 1.6in;" />
    
Now let's run the ASP.NET Core web app, and print the Razor Page to a PDF file using web browser printer. You can measure the barcode image size in the PDF file using Adobe Acrobat.





Create Barcode Image in web application through URL web request

BarcodeLib ASP.NET Barcode Generator library supports barcode generation through web url. In the following tutorial, we will quickly create a new ASP.NET Core web application to enable barcode generation through URL.



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

  1. Open Visual Studio, Create a new ASP.NET Core Empty web project. Click Next.

  2. Choose project name BarcodeLib.BarcodeGenerator.ASPNET.URL. Click Next.

  3. Choose .NET 8.0 for Framework. Click Create.


Step 2: Add barcode library to the project

  1. Go to the downloaded package /Dll/net-standard/, add the following two dlls to the ASP.NET project reference.
    • BarcodeLib.Barcode.AspNetCore.dll
    • BarcodeLib.Barcode.Common.dll
  2. In Visual Studio, install System.Drawing.Common from NuGet Package Manager


Step 3: Add C# code to enable barcode generation through url

Add the following C# code to the project file Program.cs

using BarcodeLib.Barcode.AspNetCore;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHttpContextAccessor();
var app = builder.Build();

app.MapGet("/", async (IHttpContextAccessor httpContextAccessor) =>
{
    var context = httpContextAccessor.HttpContext;
    if (context != null)
        await LinearASPNETResponse.drawBarcodeAsync(
            context.Request, context.Response);
});

app.MapGet("/QRCode", async (IHttpContextAccessor httpContextAccessor) =>
{
    var context = httpContextAccessor.HttpContext;
    if (context != null)
        await QRCodeASPNETResponse.drawBarcodeAsync(
            context.Request, context.Response);
});

app.MapGet("/DataMatrix", async (IHttpContextAccessor httpContextAccessor) =>
{
    var context = httpContextAccessor.HttpContext;
    if (context != null)
        await DataMatrixASPNETResponse.drawBarcodeAsync(
            context.Request, context.Response);
});

app.MapGet("/PDF417", async (IHttpContextAccessor httpContextAccessor) =>
{
    var context = httpContextAccessor.HttpContext;
    if (context != null)
        await PDF417ASPNETResponse.drawBarcodeAsync(
            context.Request, context.Response);
});

app.Run();


Run the app in Visual Studio. You can embed the barcode generation url in html image tag, .net reports, and Microsoft Excel Spreadsheet (web version).

Linear Barcode in Browser
QR Code in Browser


How to create barcode in ASP.NET Framework web forms application?

In this section, we will explain how to use ASP.NET Barcode library to generate barcode images in ASP.NET Framework web forms application.

BarcodeLib ASP.NET Barcode Generatoion library supports.
  • .NET Framework 4.x, 3.x and 2.x


ASP.NET Barcode Control - Barcodes Generation through ASP.NET Web Controller

  1. Install ASP.NET Barcode Controller to your ASP.NET project.
    • Add Reference BarcodeLib.Barcode.ASP.NET.dll to your project.
      There's no need to copy barcoding dll to your project bin folder because Visual Studio will do it for you during project compilation time.
  2. Add barcode library to your Visual Studio Toolbox.
    • Open Toolbox in Visual Studio. Click menu View, and check submenu Toolbox.
    • Right click Toolbox, click menu Choose Items...
    • Goto .NET Framework Components tab.
    • If no BarcodeLib component found, click Browse... button and select BarcodeLib.Barcode.ASP.NET.dll file.
    • Then sort "Namespace" column, you will find 4 components from BarcodeLib.Barcode.
    • Check component LinearASPNET, and its namespace is BarcodeLib.Barcode.
    • Check component DataMatrixASPNET, and its namespace is BarcodeLib.Barcode.
    • Check component PDF417ASPNET, and its namespace is BarcodeLib.Barcode.
    • Check component QRCodeASPNET, and its namespace is BarcodeLib.Barcode.
    • Click "OK" button, you will find four components under "General": LinearASPNET, DataMatrixASPNET, PDF417ASPNET, QRCodeASPNET.
  3. Go to "barcode" folder in the trial package, copy files "linear.aspx", "linear.aspx.cs", "datamatrix.aspx", "datamatrix.aspx.cs", "pdf417.aspx", "pdf417.aspx.cs", "qrcode.aspx", "qrcode.aspx.cs" to the same folder as your aspx page, which will generate barcodes.
  4. You can drag LinearASPNET on your aspx page, change barcode setting through properties window.
  5. Run the project, you will find barcode images generated in your aspx pages.




Barcode Generation without Web Controller

  1. Under downloaded trial package, copy barcode folder to your IIS folder, e.g. C:\Inetpub.
  2. Create a new virtual directory in IIS, named barcode, and link to the above "barcode" folder.
  3. Restart Microsoft IIS.
  4. To test your installation, open your web browser and navigate to:
    http://YourDomain:Port/barcode/linear.aspx?Type=7&Data=CODE39-123456789012
  5. To create barcode image in your aspx or html page, you need pass the url to IMG tag src value.
    For linear barcode
    <img src="http://YourDomain:port/barcode/linear.aspx?Type=7&Data=CODE39-123456789012" />
    or for Data Matrix
    <img src="http://YourDomain:port/barcode/datamatrix.aspx?Data=BarcodeLib12345678&ModuleSize=4&LeftMargin=8&RightMargin=8&TopMargin=8&BottomMargin=8" />
    or for PDF417
    <img src="http://YourDomain:port/barcode/pdf417.aspx?Data=123456789&ModuleSize=3&LeftMargin=6&RightMargin=6&TopMargin=6&BottomMargin=6" />
    or for QRCode
    <img src="http://YourDomain:port/barcode/qrcode.aspx?Data=0123456789&ModuleSize=3&LeftMargin=12&RightMargin=12&TopMargin=12&BottomMargin=12" />
    Using this method, it will not generate any barcode images in your IIS server side.


Other Related Articles