Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Products
  • Resources
    • Cheatsheets
    • Tech Comparisons
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. ASP.NET Core
  4. Barcode Scanning and Generation in ASP.NET Core with ZXing.NET

Barcode Scanning and Generation in ASP.NET Core with ZXing.NET

Date- May 27,2026 288
aspnetcore barcode

Overview

Barcodes are a method of representing data in a visual, machine-readable form. They are ubiquitous in various industries, serving as a vital tool for tracking products, managing inventory, and facilitating checkout processes. The ability to scan and generate barcodes programmatically allows developers to create efficient applications that streamline these operations, reduce human error, and enhance productivity.

The ZXing.NET library is a popular open-source library that provides robust functionality for barcode generation and scanning across multiple formats, including QR codes, Code 39, and more. By integrating ZXing.NET into an ASP.NET Core application, developers can easily create solutions that meet the demands of real-world applications, such as point-of-sale systems, warehouse management, and mobile applications.

Prerequisites

  • ASP.NET Core SDK: Ensure you have the latest version installed to build web applications.
  • Visual Studio or VS Code: A suitable IDE for writing and managing your ASP.NET Core projects.
  • NuGet Package Manager: Required to install the ZXing.Net library for barcode functionalities.
  • Basic C# Knowledge: Familiarity with C# and ASP.NET Core programming concepts is essential.

Setting Up ZXing.NET in ASP.NET Core

To utilize ZXing.NET in your ASP.NET Core application, you first need to install the library via NuGet. This library contains classes for generating and scanning barcodes, making it a comprehensive solution for barcode functionalities.

dotnet add package ZXing.Net

This command adds the ZXing.Net package to your project, enabling you to access its functionalities. Once installed, you can start using ZXing.NET to create barcodes.

Generating a Barcode

Generating a barcode is straightforward with ZXing.NET. The library provides a simple API to encode strings into various barcode formats. Below is an example of generating a barcode in an ASP.NET Core controller.

using Microsoft.AspNetCore.Mvc;
using ZXing;
using System.IO;

namespace BarcodeApp.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class BarcodeController : ControllerBase
{
[HttpGet("generate/{text}")]
public IActionResult GenerateBarcode(string text)
{
var writer = new BarcodeWriter
{
Format = BarcodeFormat.CODE_128,
Options = new ZXing.Common.EncodingOptions
{
Width = 300,
Height = 150
}
};
using var ms = new MemoryStream();
writer.Write(text).Save(ms, System.Drawing.Imaging.ImageFormat.Png);
return File(ms.ToArray(), "image/png");
}
}
}

This code defines a simple API endpoint for generating a barcode. Here’s a breakdown of the code:

  • Using Directives: The necessary namespaces are imported, including ZXing for barcode generation.
  • BarcodeController: This class is an ASP.NET Core controller that handles requests related to barcode generation.
  • GenerateBarcode Method: This method takes a string input text and generates a barcode. It uses BarcodeWriter to specify the format and dimensions of the barcode.
  • MemoryStream: The generated barcode image is saved to a MemoryStream, which is then returned as a PNG image.

Expected output: When you access the endpoint /api/barcode/generate/YourTextHere, a barcode image representing the input text is generated and returned as a PNG file.

Scanning a Barcode

Scanning a barcode is another essential feature provided by ZXing.NET. This functionality allows your application to interpret and decode barcode images. Below is an example of how to implement barcode scanning in an ASP.NET Core application.

using Microsoft.AspNetCore.Mvc;
using ZXing;
using System.Drawing;
using System.IO;

namespace BarcodeApp.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class BarcodeScannerController : ControllerBase
{
[HttpPost("scan")]
public IActionResult ScanBarcode([FromBody] byte[] imageData)
{
using var ms = new MemoryStream(imageData);
var barcodeBitmap = (Bitmap)Image.FromStream(ms);
var reader = new BarcodeReader();
var result = reader.Decode(barcodeBitmap);

if (result != null)
{
return Ok(result.Text);
}
return NotFound();
}
}
}

This code defines a controller that accepts a barcode image and decodes it. Here’s a detailed explanation:

  • ScanBarcode Method: This method accepts a byte array representing the barcode image. It converts the byte array into a Bitmap object.
  • BarcodeReader: An instance of BarcodeReader is created to decode the barcode from the bitmap.
  • Result Handling: If the barcode is successfully decoded, it returns the text contained in the barcode; otherwise, it returns a 404 Not Found response.

Expected output: When you post a barcode image to the endpoint /api/barcodeScanner/scan, the service decodes the barcode and returns the text encoded within it.

Handling Different Barcode Formats

ZXing.NET supports various barcode formats, including QR codes, Code 39, and EAN-13. Each format has its use cases and characteristics. Understanding how to specify and handle these formats is crucial for your application.

Encoding Different Formats

When generating barcodes, you can specify different formats using the BarcodeFormat enumeration. Here’s how you can extend the barcode generation example to support multiple formats.

[HttpGet("generate/{text}/{format}")]
public IActionResult GenerateBarcode(string text, string format)
{
BarcodeFormat barcodeFormat;
switch (format.ToUpper())
{
case "QR":
barcodeFormat = BarcodeFormat.QR_CODE;
break;
case "CODE39":
barcodeFormat = BarcodeFormat.CODE_39;
break;
default:
barcodeFormat = BarcodeFormat.CODE_128;
break;
}
var writer = new BarcodeWriter
{
Format = barcodeFormat,
Options = new ZXing.Common.EncodingOptions
{
Width = 300,
Height = 150
}
};
using var ms = new MemoryStream();
writer.Write(text).Save(ms, System.Drawing.Imaging.ImageFormat.Png);
return File(ms.ToArray(), "image/png");
}

This updated method allows users to specify the desired barcode format as part of the URL. The switch statement determines which format to use based on the input. This flexibility is crucial for applications that need to support multiple barcode standards.

Decoding Different Formats

When scanning barcodes, similar flexibility is necessary. The BarcodeReader can automatically detect various barcode formats, but you can specify which formats to decode for better performance.

var reader = new BarcodeReader
{
Options = new ZXing.Common.DecodingOptions
{
PossibleFormats = new List
{
BarcodeFormat.QR_CODE,
BarcodeFormat.CODE_39
}
}
};

In this example, the PossibleFormats property is set to limit the formats the reader will attempt to decode, which can improve performance in scenarios where only specific formats are expected.

Edge Cases & Gotchas

When implementing barcode scanning and generation, several pitfalls can arise. Understanding these edge cases is critical to delivering a robust solution.

Common Pitfalls

  • Incorrect Image Formats: Ensure the images being scanned are in a compatible format. For instance, JPEG images may introduce compression artifacts that can affect decoding.
  • Barcode Size: If barcodes are too small or too large, they may not be scanned correctly. Always test with various sizes to determine the optimal dimensions.
  • Unsupported Formats: When generating or scanning, ensure that the specified formats are supported by ZXing.NET. Attempting to use an unsupported format can lead to runtime errors.

Performance & Best Practices

To ensure that your barcode scanning and generation functionalities are efficient and reliable, consider the following best practices:

Use Asynchronous Programming

Barcode operations, especially scanning, can be resource-intensive. Utilize asynchronous programming in ASP.NET Core to prevent blocking calls and enhance performance. For example, wrap I/O-bound operations in async methods to improve responsiveness.

Optimize Image Processing

When processing images, ensure that you are using the most efficient image formats and sizes. Large images can slow down the decoding process. Aim for a balance between image quality and processing speed.

Testing with Various Inputs

Thoroughly test your barcode generation and scanning functionalities with different inputs, including edge cases like empty strings, malformed images, and varying barcode formats. This ensures that your application can handle a wide range of scenarios gracefully.

Real-World Scenario: Inventory Management System

Let’s tie everything together with a mini-project: an inventory management system that uses barcode scanning and generation to manage products. This system will allow users to add products, generate barcodes, and scan them to retrieve product information.

Setting Up the Project

Create a new ASP.NET Core Web API project. Define a simple model for products:

public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string Barcode { get; set; }
}

Next, create a simple in-memory repository to store products:

public class ProductRepository
{
private readonly List _products = new List();

public void Add(Product product)
{
_products.Add(product);
}

public Product GetByBarcode(string barcode)
{
return _products.FirstOrDefault(p => p.Barcode == barcode);
}
}

Now create a controller to manage products:

using Microsoft.AspNetCore.Mvc;
using ZXing;
using System.IO;
using System.Collections.Generic;

namespace InventoryApp.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ProductsController : ControllerBase
{
private readonly ProductRepository _repository = new ProductRepository();

[HttpPost("add")]
public IActionResult AddProduct([FromBody] Product product)
{
var writer = new BarcodeWriter
{
Format = BarcodeFormat.CODE_128,
Options = new ZXing.Common.EncodingOptions
{
Width = 300,
Height = 150
}
};
using var ms = new MemoryStream();
writer.Write(product.Name).Save(ms, System.Drawing.Imaging.ImageFormat.Png);
product.Barcode = Convert.ToBase64String(ms.ToArray());
_repository.Add(product);
return CreatedAtAction(nameof(GetByBarcode), new { barcode = product.Barcode }, product);
}

[HttpGet("{barcode}")]
public IActionResult GetByBarcode(string barcode)
{
var product = _repository.GetByBarcode(barcode);
if (product == null)
{
return NotFound();
}
return Ok(product);
}
}
}

This controller provides two endpoints: one for adding products and generating barcodes and another for retrieving products by scanning their barcodes. The AddProduct method generates a barcode based on the product name and stores it as a Base64 string. The GetByBarcode method retrieves the product information based on the scanned barcode.

Conclusion

  • Understanding how to implement barcode scanning and generation enhances the functionality of ASP.NET Core applications.
  • ZXing.NET is a powerful library that simplifies barcode operations.
  • Consider performance and edge cases when implementing barcode functionalities.
  • Testing with real-world scenarios ensures robustness in your application.
  • Explore further with advanced topics such as integrating with mobile applications for barcode scanning.

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Integrating Plivo SMS API with ASP.NET Core: A Comprehensive Guide
Apr 29, 2026
Integrating Fast2SMS with ASP.NET Core for Reliable SMS Delivery in India
Apr 28, 2026
Building a RESTful Web API with ASP.NET Core: A Comprehensive Guide
Mar 16, 2026
Implementing API Key Authentication Middleware in ASP.NET Core Web API
Jun 10, 2026
Previous in ASP.NET Core
Zapier Webhook Integration in ASP.NET Core - Trigger Automation W…
Next in ASP.NET Core
Integrating Currency and Exchange Rate API in ASP.NET Core for Re…
Buy me a pizza

Comments

🔥 Trending This Month

  • 1
    CWE-269: Improper Privilege Management - Implementing the … 332 views
  • 2
    Building Custom Bedrock Add-Ons with JavaScript: A Complet… 1,931 views
  • 3
    Error-An error occurred while processing your request in .… 11,954 views
  • 4
    Elasticsearch Integration in ASP.NET Core - Full-Text Sear… 240 views
  • 5
    Send Email With HTML Template And PDF Using ASP.Net C# 17,176 views
  • 6
    Fix Gemini API Error 429: Quota Exceeded on Free Tier (Sys… 818 views
  • 7
    Integrating Google reCAPTCHA Validation in ASP.NET MVC 6,458 views

On this page

🎯

Interview Prep

Ace your ASP.NET Core interview with curated Q&As for all levels.

View ASP.NET Core Interview Q&As

More in ASP.NET Core

  • How to Encrypt and Decrypt Password in Asp.Net 26677 views
  • Exception Handling Asp.Net Core 21715 views
  • HTTP Error 500.31 Failed to load ASP NET Core runtime 21169 views
  • How to implement Paypal in Asp.Net Core 20126 views
  • Task Scheduler in Asp.Net core 18197 views
View all ASP.NET Core posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1780
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Products
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor