How to Generate Barcode in ASP.NET using C#

Barcode for ASP.NET - How to Use C# Code to Create Barcode Images for ASP.NET Web Projects
  • Easy to install BarcodeLib.Barcode.ASP.NET.dll to your Visual Studio ASP.NET Web application
  • Easy to encode and generate linear & 2d barcodes using C# demo code
  • Use C# code to create and print barcode images on web pages or your IIS projects
  • Use C# code to customize your created barcode image properties
  • Use C# code to save barcode images in the memory or stream to a web page
  • Able to generate more than 20 common barcodes, like Codabar, Code 39, Code 128, EAN/UPC,
    Postal Barcode, QR-Code, Data Matrix, PDF-417 and so on

C# Barcode Generator Library

QR Code, Data Matrix, Code 128, GS1, EAN/UPC. Unicode text encoding supported.

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


ASP.NET C# Barcode Generation Prerequisites

  • BarcodeLib.Barcode.ASP.NET.dll
  • Microsoft .NET Framework 4.x
  • Visual Studio .NET 2022


Create an ASP.NET Framework application to print barcode on web page

In the following step-by-step C# tutorial, we will create a new ASP.NET framework web application to generate, print QR Code, Code 128 and other barcodes on the web page using Visual Studio 2022.

In the end of the ASP.NET tutorial, you can create and print barcodes on the ASP.NET web page in browser.



1. Create a new ASP.NET framework project

  1. Create a new ASP.NET Web Application (.NET Framework) project in Visual Studio 2022.

  2. Input Project name as BarcodeLib.BarcodeGenerator.ASPNETFrameworkDemo, and choose Framework as .NET Framework 4.7.2

  3. Choose an Empty asp.net web application.

  4. Now a new ASP.NET web application with default files are generated.


2. Add C# Barcode Library dll to the project

Here we will add BarcodeLib C# Barcode Generator for ASP.NET dll to the new web project.
  • Add downloaded dll BarcodeLib.Barcode.ASP.NET.dll (.net framework 4.0 version from folder /Dll/net40) to the ASP.NET project reference.


3. Create, customize web control on the ASP.NET web form

  1. Add a new aspx Web Form item to the project, named Default.aspx
  2. Replace the following content on file Default.aspx. Here we have added three aspx controls on the web form:
    a text box (ID: tbBarcodeData); a button (ID: btnBarcodeCreate); an image control (ID: imgBarcode).

    <%@ Page Language="C#" AutoEventWireup="true" 
        CodeBehind="Default.aspx.cs" Inherits="BarcodeLib.BarcodeGenerator.ASPNETFrameworkDemo.Default" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    
    <style>
    body {
        font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
        color: #333;
    }
    
    .center-container {
        text-align: center;
        margin: 0 auto;
        padding: 20px;
    }
    
    .center-container h2 {
        text-align: center;
    }
    
    .center-container p {
        text-align: center;
    }
    .center-image {
        display: block;
        margin: 0 auto;
    }
    </style>
    </head>
    <body>
        <div style="max-width: 1200px; margin: 0 auto; align-content:center">
        <form id="form1" runat="server">
            <div class="center-container">
                <h2>BarcodeLib C# Barcode Generator for <br />
                    ASP.NET Framework (4.x) Web Application</h2>
                <br />
                <p>
            Barcode data:  
            <asp:TextBox ID="tbBarcodeData" runat="server" Width="600px" Text="Code 128"></asp:TextBox>
               
            <br />
            <br />
            <asp:Button ID="btnBarcodeCreate" runat="server" Text="Create Barcode" CssClass="center-image"
                OnClick="ButtonBarcodeCreate_Click" />
    
    
            <br /><br />
    
            <asp:Image ID="imgBarcode" runat="server" AlternateText="Barcode Image" 
                 />
    
                     </p>
            </div>
        </form>
        </div>
    </body>
    </html>
    


4. Add barcode generation C# code to the ASPX cs file

Replace the following content on file Default.aspx.cs. In the following C# code, we will create a Code 128 barcode image with the user input text, and print the generated barcode image on the web form.

using BarcodeLib.Barcode;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BarcodeLib.BarcodeGenerator.ASPNETFrameworkDemo
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ButtonBarcodeCreate_Click(object sender, EventArgs e)
        {
            string barcodeData = tbBarcodeData.Text;

            Linear barcode = new Linear();
            barcode.Type = BarcodeType.CODE128;
            barcode.Data = barcodeData;

            byte[] imageBytes = barcode.drawBarcodeAsBytes();
            string base64String = Convert.ToBase64String(imageBytes);

            imgBarcode.ImageUrl = "data:image/png;base64," + base64String;
        }
    }
}


5. Run the ASP.NET web application

Now we can run the web application in Visual Studio, and generate, print barcodes on the web page in browser.



Create Barcode Image in ASP.NET Web Application in C#

Install

  1. Add Reference BarcodeLib.Barcode.ASP.NET.dll component to your asp.net website project reference.

Implement

Here is the Visual C#.NET sample code for you to generate QR Code barcode images in C# Class Library. Please to your Visual Studio ASP.NET web applications for a barcoding test.
     BarcodeLib.Barcode.Linear barcode = new BarcodeLib.Barcode.Linear();
barcode.Type = BarcodeType.CODE128;
barcode.Data = "CODE128";

barcode.LeftMargin = 0;
barcode.RightMargin = 0;
barcode.TopMargin = 0;
barcode.BottomMargin = 0;

barcode.Resolution = 72;
barcode.Rotate =RotateOrientation.BottomFacingDown;

barcode.UOM = UnitOfMeasure.PIXEL;

// other barcode settings.

// save barcode image into your system
barcode.drawBarcode("c:/csharp-barcode.png");

// generate barcode & output to byte array
byte[] barcodeInBytes = barcode.drawBarcodeAsBytes();

// generate barcode to <strong>Graphics object<strong>
Graphics graphics = ...
barcode.drawBarcode(graphics);

// generate barcode and output to Bitmap object
Bitmap barcodeInBitmap = barcode.drawBarcode();

// generate barcode and output to HttpResponse object
HttpResponse response = ...;
barcode.drawBarcode(response);

// generate barcode and output to Stream object
Stream stream = ...;
barcode.drawBarcode(stream);


Other Methods to Create Barcodes in C# ASP.NET

Stream Barcodes with Buildin ASP.NET Barcode Application

Developers can stream barcode images with Buildin ASP.NET Barcode Application. Using this method you can create barcodes with a single URL through Microsoft Internet Information Service (IIS). Generated barcode images can be saved for future use, and you just need to pass the url to img tag.
< div class="bl_paragraph"> This method is the easiest way as you can create dynamic barcode images, and adjust barcode settings through the url. View How to Stream Barcodes with Building ASP.NET Barcode Application

Generate Barcodes with Web Controller

  1. To use this barcoding method, you need to add BarcodeLib.Barcode.ASP.NET.dll component to your ASP.NET project.
  2. Copy the .aspx and the .aspx.cs files you need to the website where you will be generating barcodes.
  3. Now please add the asp.net barcode control to VS toolbox.



  4. Simply drag the control to your website. For example, QRCodeASPNET.
  5. Debug and a QR Code bar code is generated on the ASP.NET web site!
ASP.NET Barcode Generator Library - Supported Barcodes
  • Generate Linear Barcode Images in ASP.NET
    ASP.NET Linear Barcodes:


  • Generate 2D Barcode Images in ASP.NET
    ASP.NET Matrix Barcodes:

Other Related .NET Barcode Generator SDKs