DEV Community

Mehr Muhammad Hamza
Mehr Muhammad Hamza

Posted on • Edited on

How to generate Barcodes in C#: Full tutorial

Working with barcodes in .NET used to be tedious — limited format support, low-quality output, and complex APIs. IronBarcode changes all that. With high accuracy, broad format compatibility, and an intuitive API, it's one of the best libraries for .NET developers who need to generate or read barcodes with minimal effort.

This article shows how to create, style, save, and decode barcodes in C# using IronBarcode. It covers common use cases, advanced styling options, and even how to read barcodes from files, streams, and PDFs — all while keeping performance and clarity in mind.

🔒 Note: IronBarcode is a commercial library developed by Iron Software. It offers a fully functional free trial so developers can evaluate it before purchasing. Learn more about pricing and licensing here.

Prerequisites

Before writing any code, let’s make sure your environment is compatible:

.NET Compatibility: IronBarcode supports:

✅ .NET 9, 8, 7, 6, 5
✅ .NET Core 2.0+
✅ .NET Standard 2.0+
✅ .NET Framework 4.6.2+

This wide compatibility ensures it works in both modern applications (e.g., Blazor, MAUI, ASP.NET Core) and legacy systems running on older frameworks.

Development Tools: IronBarcode works with any .NET-compatible IDE, but Visual Studio 2019 or 2022 is commonly used due to its powerful IntelliSense and debugging capabilities.

Create a Visual Studio Project

  1. Start by creating a new project in Visual Studio.
    1. Open Visual Studio 2022.
    2. Choose a project template (e.g., Console Application).
    3. Configure project settings (name, location).
    4. Select the target framework (e.g., .NET 8).

Visual Studio Project

Installation

The NuGet package name for IronBarcode is simply BarCode, published by Iron Software.

Install via NuGet Package Manager Console:

Install-Package Barcode
Enter fullscreen mode Exit fullscreen mode

To install the library via the NuGet Package Manager window, go to Tools>NuGet Package Manager>Manage NuGet Packages for Solution and search for IronBarcode.

NuGet Package Manager For Solution

Install via .NET CLI:

dotnet add package BarCode
Enter fullscreen mode Exit fullscreen mode

🆕 Latest Version: Check the NuGet page for the latest release. At the time of writing, the version is 2025.4.2.

After installation, add the necessary namespace to your project. Ensure the correct namespace is used:

using IronBarCode;
Enter fullscreen mode Exit fullscreen mode

Generating Barcodes in C

To generate a barcode, use the BarcodeWriter.CreateBarcode() method. It supports popular formats such as Code128, QRCode, UPC, EAN, PDF417, DataMatrix, and many more.

Basic Example:

Here’s how to generate a simple barcode and save it as an image using default settings.

using IronBarCode;

var barcode = BarcodeWriter.CreateBarcode("123456789", BarcodeEncoding.Code128);
barcode.SaveAsImage("barcode.png");

Enter fullscreen mode Exit fullscreen mode

This creates a Code128 barcode with the text "123456789" and saves it as a PNG file. The method SaveAsImage("filename.ext") intelligently determines the format from the extension.

Barcode Image File

Saving Barcodes: Flexible Output Options

IronBarcode supports multiple output formats. You can either specify the format implicitly via the file extension or explicitly using specific save methods.

Recommended Way:

Automatically selects the format based on the file extension.

barcode.SaveAsImage("barcode.jpg"); // Saves based on file extension
Enter fullscreen mode Exit fullscreen mode

Explicit Format Options:

Each of these methods saves the barcode in the respective file format.

barcode.SaveAsPng("barcode.png");
barcode.SaveAsJpeg("barcode.jpeg");
barcode.SaveAsGif("barcode.gif");
barcode.SaveAsTiff("barcode.tiff");
barcode.SaveAsPdf("barcode.pdf");
barcode.SaveAsHtmlFile("barcode.html");
Enter fullscreen mode Exit fullscreen mode

These options make it easy to integrate barcode generation into various applications such as:

  • Printing documents (PDF)
  • Embedding in HTML reports
  • Mobile/web previews (PNG, JPEG, etc.)

Styling Barcodes: Size, Color & Margins

IronBarcode provides styling methods to adjust colors, size, and layout.

🎨 Note: To style barcodes with custom colors, you must install the additional NuGet package:

Install-Package IronSoftware.System.Drawing

This library provides the IronSoftware.System.Drawing.Color struct used in color-related methods.

Color Customization (Correct Namespace!)

To apply background and foreground colors, use the color struct from the correct namespace.

using IronBarCode;
using IronSoftware.System.Drawing; // Required for color customization

// Create a QR code and apply custom colors
var barcode = BarcodeWriter.CreateBarcode("Styled Text", BarcodeEncoding.QRCode);
barcode.ChangeBackgroundColor(Color.LightYellow);
barcode.ChangeBarCodeColor(Color.DarkSlateGray, true);
barcode.SaveAsImage("colored_barcode.png");
Enter fullscreen mode Exit fullscreen mode

Styled Barcode

Resizing the Output:

You can resize the barcode to match a specific width and height. This is particularly useful for printing or web display.

barcode.ResizeTo(400, 150); // Width x Height
Enter fullscreen mode Exit fullscreen mode

Adding Margins:

Adding extra space around the barcode can help ensure proper scanning in various environments.

barcode.AddMarginPixels(20); // Adds 20px white space on all sides
Enter fullscreen mode Exit fullscreen mode

Add white padding around the barcode — useful for scanners that require breathing room.

Fluent API Style

To keep your code concise and expressive, IronBarcode supports method chaining via a Fluent API:

        BarcodeWriter.CreateBarcode("Fluent Style", BarcodeEncoding.Code128)
.ResizeTo(300, 100)
.AddAnnotationTextAboveBarcode("Fluent API Example")
.SaveAsImage("fluent_example.png");
Enter fullscreen mode Exit fullscreen mode

This approach is elegant and especially helpful when generating many barcodes dynamically.

Reading Barcodes in C#

Reading a barcode is straightforward. Use the BarcodeReader.Read() method with the path to your image file.

  var barcodeList = BarcodeReader.Read("barcode.png");
  foreach (var barcode in barcodeList) {
      Console.WriteLine(barcode.Value);
          }
Enter fullscreen mode Exit fullscreen mode

Input Flexibility

IronBarcode can read from multiple sources:

✅ File Paths (.png, .jpg, .tiff, .bmp, .gif)
✅ Bitmap Objects
✅ Streams (e.g., memory streams or network streams)
✅ PDF Documents (BarcodeReader.ReadPdf("file.pdf"))

This makes it adaptable to web uploads, scanned documents, and mobile-captured images.

BarcodeReaderOptions – Read with Control

To improve scan reliability and reduce false positives, configure how the barcode is read using BarcodeReaderOptions.

var options = new BarcodeReaderOptions
{
    Speed = ReadingSpeed.Balanced,
    ExpectMultipleBarcodes = true,
    ExpectBarcodeTypes = BarcodeEncoding.All
};

var results = BarcodeReader.Read("multi_codes.png", options);

foreach (var code in results)
{
    Console.WriteLine(code.Text);
}
Enter fullscreen mode Exit fullscreen mode
  • Speed: Choose between Detailed, Balanced, or Faster
  • ExpectMultipleBarcodes: Enables multi-code reading from one image
  • ExpectBarcodeTypes: Optimize by targeting specific barcode types

Cross-Platform & Deployment Compatibility

IronBarcode is designed for a wide range of deployment environments:

Operating Systems:

  • Windows
  • Linux
  • macOS

Cloud & Containers:

  • Docker
  • Azure Functions
  • AWS Lambda

Frameworks:

  • .NET 8, 7, 6, 5
  • .NET Core 2.x+
  • .NET Standard 2.0+
  • .NET Framework 4.6.2+

Licensing

IronBarcode is a commercial product offered by Iron Software. Here's a quick licensing breakdown:

  • Model: Per developer, per project, per deployment location
  • Starting Price: $749 USD (Lite License)
  • Trial: fully featured free trial — no credit card required
  • Use Cases: Ideal for commercial software, internal tools, and enterprise applications

🔗 Learn more: IronBarcode Licensing Page

⚠️ Always verify license terms for production environments. Contact Iron Software for custom licensing options if your usage involves redistribution, SaaS platforms, or volume deployment.

Conclusion

IronBarcode is a robust and production-grade library for any .NET developer needing barcode functionality. It combines:

Ease of use – Simple methods with sensible defaults
High accuracy – Barcode recognition works across image qualities and formats
Versatility – Create and read dozens of barcode types with full styling control
Enterprise support – Commercial licensing, responsive support, and regular updates

Whether you're building a retail POS, inventory app, document tracker, or any barcode-integrated system, IronBarcode saves you time, increases reliability, and scales with your needs.

Further Resources

🌐 IronBarcode Product Page
📘 IronBarcode Documentation
🔧 Barcode Generator Examples (C#)
📦 BarCode on NuGet
📄 Licensing Info

Top comments (0)