Skip to footer content
USING IRONBARCODE

ASP .NET Barcode Generation: Build a Barcode Generator for Your Web Application

Integrating barcode generation into an ASP.NET web application opens powerful capabilities for inventory management, product tracking, and document automation. This article demonstrates how to generate barcodes in a .NET Core project using a robust barcode generator library that supports multiple formats including QR Code, Data Matrix, Code 128, and UPC A.

Start your free trial to follow along with the tutorial code examples below.

How Do I Install a Barcode Library in My .NET Core Project?

Adding barcode generation capabilities to your ASP.NET Core web application requires installing the IronBarcode NuGet package. Open Visual Studio and use the Package Manager Console to install the dependencies:

Install-Package BarCode

The package is compatible with .NET Core 6, 7, 8, and newer versions, as well as .NET Framework 4.6.2+. The library's core DLL handles all dependencies automatically, requiring no additional barcode fonts or system configuration. Each version supports Windows, and the software works seamlessly across different .NET environments. Once installed, add the namespace reference to your controller or service file:

using IronBarCode;
using IronBarCode;
Imports IronBarCode
$vbLabelText   $csharpLabel

How Can I Generate Barcodes in an ASP.NET Core Web Application?

Creating barcodes in an MVC controller involves using the BarcodeWriter class. The following example demonstrates a complete controller action that creates a Code 128 barcode based on a URL or text provided by the user:

using IronBarCode;
using Microsoft.AspNetCore.Mvc;
public class BarcodeController : Controller
{
    public IActionResult GenerateBarcode(string data)
    {
        // Generate barcode from input data
        GeneratedBarcode barcode = BarcodeWriter.CreateBarcode(
            data,
            BarcodeWriterEncoding.Code128);
        barcode.ResizeTo(400, 120);
        barcode.AddBarcodeValueTextBelowBarcode();
        // Return as image for display in browser
        byte[] barcodeBytes = barcode.ToPngBinaryData();
        return File(barcodeBytes, "image/png");
    }
}
using IronBarCode;
using Microsoft.AspNetCore.Mvc;
public class BarcodeController : Controller
{
    public IActionResult GenerateBarcode(string data)
    {
        // Generate barcode from input data
        GeneratedBarcode barcode = BarcodeWriter.CreateBarcode(
            data,
            BarcodeWriterEncoding.Code128);
        barcode.ResizeTo(400, 120);
        barcode.AddBarcodeValueTextBelowBarcode();
        // Return as image for display in browser
        byte[] barcodeBytes = barcode.ToPngBinaryData();
        return File(barcodeBytes, "image/png");
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Output Barcode Image

ASP .NET Barcode Generation: Build a Barcode Generator for Your Web Application: Image 1 - Generated Barcode

The BarcodeWriter.CreateBarcode() method accepts barcode data as the first parameter and the encoding format as the second. This approach lets you create barcodes dynamically based on user input or database values. Unlike some PHP implementations that require external GD libraries, this .NET solution is self-contained.

For saving barcode images to your project folder, use the SaveAsImage() method:

barcode.SaveAsImage("wwwroot/images/generated-barcode.png");
barcode.SaveAsImage("wwwroot/images/generated-barcode.png");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

The library supports multiple output formats including PNG, GIF, BMP, SVG, and JPEG. You can also export barcodes as HTML for embedding directly into web pages.

What Barcode Types Are Supported for Web Applications?

The barcode generator supports over 30 formats. Here are the details on how to create the most commonly used types:

QR Code Generation

// Create QR Code with custom size
GeneratedBarcode qrCode = QRCodeWriter.CreateQrCode(
    "https://example.com/product/12345",
    500,
    QRCodeWriter.QrErrorCorrectionLevel.Medium);
qrCode.SaveAsPng("product-qr.png");
// Create QR Code with custom size
GeneratedBarcode qrCode = QRCodeWriter.CreateQrCode(
    "https://example.com/product/12345",
    500,
    QRCodeWriter.QrErrorCorrectionLevel.Medium);
qrCode.SaveAsPng("product-qr.png");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Output QR Code

ASP .NET Barcode Generation: Build a Barcode Generator for Your Web Application: Image 2 - Image 2 of 6 related to ASP .NET Barcode Generation: Build a Barcode Generator for Your Web Application

QR Code generation supports error correction levels that determine how much of the code can be damaged while remaining readable. Use Medium or High for print QR code applications where physical wear is expected. Learn more about QR Code customization options.

Data Matrix and Other 2D Formats

// Create Data Matrix barcode
GeneratedBarcode dataMatrix = BarcodeWriter.CreateBarcode(
    "DMX-2024-001",
    BarcodeWriterEncoding.DataMatrix);
dataMatrix.SaveAsGif("datamatrix.gif");
// Create Data Matrix barcode
GeneratedBarcode dataMatrix = BarcodeWriter.CreateBarcode(
    "DMX-2024-001",
    BarcodeWriterEncoding.DataMatrix);
dataMatrix.SaveAsGif("datamatrix.gif");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Output Data Matrix

ASP .NET Barcode Generation: Build a Barcode Generator for Your Web Application: Image 3 - Generated Data Matrix

Data Matrix barcodes excel in scenarios requiring compact, high-density encoding. The library also generates PDF417, Aztec, and MaxiCode formats. These 2D barcode types store significantly more data than linear formats while maintaining reliable scan accuracy.

Linear Barcodes (UPC, EAN, Code 39)

// Create UPC-A barcode for retail products
GeneratedBarcode upcBarcode = BarcodeWriter.CreateBarcode(
    "012345678905",
    BarcodeWriterEncoding.UPCA);
// Create UPC-A barcode for retail products
GeneratedBarcode upcBarcode = BarcodeWriter.CreateBarcode(
    "012345678905",
    BarcodeWriterEncoding.UPCA);
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Output

ASP .NET Barcode Generation: Build a Barcode Generator for Your Web Application: Image 4 - Generated barcode

UPC A and EAN barcodes include automatic checksum validation, preventing encoding errors in your barcode generation process.

How Do I Customize and Export Barcode Images?

Styling barcodes for your web application involves adjusting colors, fonts, margins, and annotations. The fluent API makes customization straightforward:

GeneratedBarcode styledBarcode = BarcodeWriter.CreateBarcode(
    "STYLED-2024",
    BarcodeWriterEncoding.Code128);
// Apply styling
styledBarcode.ResizeTo(450, 150);
styledBarcode.SetMargins(20);
styledBarcode.ChangeBarCodeColor(IronSoftware.Drawing.Color.DarkBlue);
styledBarcode.AddAnnotationTextAboveBarcode("Product ID:");
styledBarcode.AddBarcodeValueTextBelowBarcode();
// Export to multiple formats
styledBarcode.SaveAsPng("styled-barcode.png");
styledBarcode.SaveAsPdf("styled-barcode.pdf");
GeneratedBarcode styledBarcode = BarcodeWriter.CreateBarcode(
    "STYLED-2024",
    BarcodeWriterEncoding.Code128);
// Apply styling
styledBarcode.ResizeTo(450, 150);
styledBarcode.SetMargins(20);
styledBarcode.ChangeBarCodeColor(IronSoftware.Drawing.Color.DarkBlue);
styledBarcode.AddAnnotationTextAboveBarcode("Product ID:");
styledBarcode.AddBarcodeValueTextBelowBarcode();
// Export to multiple formats
styledBarcode.SaveAsPng("styled-barcode.png");
styledBarcode.SaveAsPdf("styled-barcode.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Styled Barcode Image

ASP .NET Barcode Generation: Build a Barcode Generator for Your Web Application: Image 5 - Output styled barcode

The SetMargins() method adds white space around the barcode for better scanner readability. You can adjust font settings for annotation text, change the font family and font size, and customize barcode styling extensively. The library renders text using system fonts without requiring external barcode fonts to be installed.

For ASP.NET MVC views, generate barcodes as Base64 strings for direct HTML embedding:

public IActionResult DisplayBarcode()
{
    GeneratedBarcode barcode = BarcodeWriter.CreateBarcode(
        "INV-2024-0042",
        BarcodeWriterEncoding.Code128);
    string base64Image = barcode.ToDataUrl();
    ViewBag.BarcodeImage = base64Image;
    return View();
}
public IActionResult DisplayBarcode()
{
    GeneratedBarcode barcode = BarcodeWriter.CreateBarcode(
        "INV-2024-0042",
        BarcodeWriterEncoding.Code128);
    string base64Image = barcode.ToDataUrl();
    ViewBag.BarcodeImage = base64Image;
    return View();
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Displayed Barcode

ASP .NET Barcode Generation: Build a Barcode Generator for Your Web Application: Image 6 - Barcode displayed in our browser

In your Razor view, display it within a <div> element:

<div class="barcode-container">
    <img src="@ViewBag.BarcodeImage" alt="Generated Barcode" />
</div>
<div class="print-actions">
    <button onclick="window.print()">Print Barcode</button>
</div>
<div class="barcode-container">
    <img src="@ViewBag.BarcodeImage" alt="Generated Barcode" />
</div>
<div class="print-actions">
    <button onclick="window.print()">Print Barcode</button>
</div>
HTML

This approach eliminates the need to save barcode images to the file system, keeping your web service stateless. You can adjust the output resolution for print quality or screen display as needed. The complete API reference documents all available methods and properties.

Summary

Building an ASP.NET barcode generator with IronBarcode provides a reliable solution for creating, styling, and exporting barcode images in your .NET Core web application. The library handles complex encoding standards automatically while offering extensive customization through a developer-friendly API. Whether generating QR codes for mobile scanning, Data Matrix barcodes for industrial applications, or UPC codes for retail, the software integrates seamlessly with ASP.NET MVC controllers and Razor pages.

You can test the complete feature set during your free evaluation period. The library supports bitmap and vector exports, with each version maintaining compatibility across Windows, Android, and iOS deployment targets. For production deployment and commercial use, view licensing options to select the appropriate license for your project requirements.

Get stated with IronBarcode now.
green arrow pointer

Frequently Asked Questions

What are the benefits of integrating barcode generation into an ASP.NET Core web application?

Integrating barcode generation into an ASP.NET Core web application enhances capabilities like inventory management, product tracking, and document automation, providing a seamless experience for handling data efficiently.

Which barcode formats are supported by the barcode generator library?

The barcode generator library supports multiple formats including QR Code, Data Matrix, Code 128, and UPC A, allowing for versatile use across various applications.

Can I generate QR codes in my .NET Core project using IronBarcode?

Yes, IronBarcode allows you to generate QR codes in your .NET Core project, providing complete C# code examples for seamless integration.

How can barcodes improve inventory management in a web application?

Barcodes can streamline inventory management by enabling quick and accurate tracking of products, reducing manual entry errors, and improving operational efficiency within a web application.

Is it possible to automate document processes using barcodes in ASP.NET?

Yes, barcodes can be used to automate document processes in ASP.NET applications by encoding necessary information that can be scanned and processed automatically, saving time and reducing errors.

What is required to start generating barcodes in an ASP.NET Core MVC project?

To start generating barcodes in an ASP.NET Core MVC project, you'll need to integrate a barcode generator library like IronBarcode, which provides comprehensive C# examples and supports various barcode formats.

Can IronBarcode handle both 1D and 2D barcode formats?

Yes, IronBarcode is capable of handling both 1D and 2D barcode formats, making it a versatile choice for different application needs.

How does barcode generation support product tracking in web applications?

Barcode generation supports product tracking by providing a unique identifier for each item, which can be easily scanned to update and retrieve product information in real-time within web applications.

Jordi Bardia
Software Engineer
Jordi is most proficient in Python, C# and C++, when he isn’t leveraging his skills at Iron Software; he’s game programming. Sharing responsibilities for product testing, product development and research, Jordi adds immense value to continual product improvement. The varied experience keeps him challenged and engaged, and he ...
Read More