IRONSOFTWAREHOME

Create Barcode from Text, URLs, IDs & Binary Data in C#

Hairil Hasyimi Bin Omar
Hairil Hasyimi Bin Omar
Updated: August 2, 2026

IronBarcode enables C# developers to generate barcodes from various data sources including strings, byte arrays, and memory streams using the BarcodeWriter.CreateBarcode() method with support for multiple barcode formats like QR Code, Code128, and PDF417.

Quickstart: Create a Barcode from String in One Line

Use IronBarcode's API to generate barcodes with minimal setup. This example shows how to create a barcode from a simple string using just one line of code. For comprehensive examples, check the Barcode Quickstart guide.

  1. 1Install IronBarcode with NuGet Package Manager

    PM > Install-Package BarCode

  2. 2Copy and run this code snippet.

    var barcode = IronBarCode.BarcodeWriter.CreateBarcode("Order123", IronBarCode.BarcodeWriterEncoding.Code128);
    C#
  3. 3Deploy to test on your live environment

    Start using IronBarcode in your project today with a free trial
    arrow pointer

How Do I Create Barcode From String?

Which Barcode Formats Work Best for Different String Types?

Different barcode formats are optimized for specific data types and use cases. Understanding supported barcode formats helps select the right encoding:

  • QR Codes: Best for URLs, email addresses, and large text data. Supports up to 4,296 alphanumeric characters with error correction.
  • Code128: Ideal for alphanumeric data like order numbers and serial codes. Highly efficient for modern applications.
  • PDF417: Perfect for complex data like flight tickets and government IDs. Stores up to 1,850 alphanumeric characters.
  • Code93: Excellent for postal services and inventory tracking with compact numeric data.
  • Aztec: Optimal for mobile ticketing and transportation, requiring less space than QR codes.

The following code demonstrates how to write barcodes with a string:

using IronBarCode;

string text = "Hello, World!";
string url = "https://ironsoftware.com/csharp/barcode/";
string receiptID = "2023-08-04-12345"; // Receipt ID (numeric id)
string flightID = "FLT2023NYC-LAX123456"; // Flight ID (alphanumeric id)
string number = "1234";

BarcodeWriter.CreateBarcode(text, BarcodeEncoding.Aztec).SaveAsPng("text.png");
BarcodeWriter.CreateBarcode(url, BarcodeEncoding.QRCode).SaveAsPng("url.png");
BarcodeWriter.CreateBarcode(receiptID, BarcodeEncoding.Code93, 250, 67).SaveAsPng("receiptID.png");
BarcodeWriter.CreateBarcode(flightID, BarcodeEncoding.PDF417, 250, 67).SaveAsPng("flightID.png");
BarcodeWriter.CreateBarcode(number, BarcodeEncoding.Codabar, 250, 67).SaveAsPng("number.png");

What Are the Generated Barcode Results?

This code encodes five different data examples into five barcode types: simple text to Aztec, URL to QR Code, numeric ID to Code 93, alphanumeric ID to PDF417, and number to Codabar. Images are saved as PNG. For advanced export options, see the Create Barcode as Image guide.

Aztec barcode containing 'Hello, World!' text with characteristic square spiral pattern
QR code generated from URL input demonstrating barcode creation functionality
Generated Code93 barcode example showing vertical black and white bars pattern
PDF417 barcode with stacked rows encoding flight ID alphanumeric data
Codabar barcode displaying numeric data with start/stop characters

How Can I Customize Generated Barcodes?

After creating your barcode, enhance its appearance using IronBarcode's styling features. Here's how to create a customized barcode with colors, annotations, and margins:

using IronBarCode;

// Create a barcode with custom styling
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("PRODUCT-12345", BarcodeEncoding.Code128);

// Apply custom styling
myBarcode.ResizeTo(300, 100);
myBarcode.SetMargins(10);
myBarcode.ChangeBarCodeColor(Color.DarkBlue);

// Add text annotations
myBarcode.AddBarcodeValueTextBelowBarcode();
myBarcode.AddAnnotationTextAboveBarcode("Product SKU", Font.Arial, Color.Black, 12);

// Save the customized barcode
myBarcode.SaveAsPng("customized-barcode.png");

For more styling options, explore the Customize and Style Barcodes tutorial.

How Do I Create Barcode From Byte Array?

Why Does Character Encoding Matter for Byte Array Barcodes?

To create barcodes from byte arrays, ensure character encoding aligns with the required BarcodeEncoding, as each barcode type accepts different character encoding. Understanding output data formats ensures compatibility. Here are the character encodings available in IronBarcode:

  • ASCII: Uses 7 bits per character for English letters, digits, and punctuation. Example: 'A' = 65.
  • UTF-8: Variable-length encoding for all Unicode characters. Example: € = 0xE2 0x82 0xAC.
  • UTF-16: Uses 16-bit sequences for Unicode. Example: a = 0x03B1.
  • UTF-32: Fixed 32-bit sequence per character. Example: a = 0x000003B1.
  • ISO-8859-1: Extends ASCII for Western European languages. Example: é = 233.

[[i:The default character encoding in IronBarcode is ISO-8859-1.]]

How Do I Convert Byte Arrays to Barcodes?

The following code demonstrates generating a barcode from byte data:

using IronBarCode;
using System.Text;

byte[] text = Encoding.UTF8.GetBytes("Hello, World!");
byte[] url = Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/barcode/");
byte[] receiptID = Encoding.UTF8.GetBytes("2023-08-04-12345"); // Receipt ID (numeric id)
byte[] flightID = Encoding.UTF8.GetBytes("FLT2023NYC-LAX123456"); // Flight id (alphanumeric id)
byte[] number = Encoding.UTF8.GetBytes("1234");

BarcodeWriter.CreateBarcode(text, BarcodeEncoding.Aztec).SaveAsPng("text.png");
BarcodeWriter.CreateBarcode(url, BarcodeEncoding.QRCode).SaveAsPng("url.png");
BarcodeWriter.CreateBarcode(receiptID, BarcodeEncoding.Code93, 250, 67).SaveAsPng("receiptID.png");
BarcodeWriter.CreateBarcode(flightID, BarcodeEncoding.PDF417, 250, 67).SaveAsPng("flightID.png");
BarcodeWriter.CreateBarcode(number, BarcodeEncoding.Codabar, 250, 67).SaveAsPng("number.png");

This snippet transforms five string inputs into System.Byte[] objects. To convert these byte arrays into barcodes, pass them to BarcodeWriter with the desired BarcodeEncoding. Optionally, set MaxWidth and MaxHeight for barcode size.

Working with Binary Data and Special Characters

When working with binary data or special characters, use Writing Unicode Barcodes for international character support. Here's an example handling binary data:

using IronBarCode;
using System.Text;
using System.IO;

// Example: Encoding binary data (like a small file) into QR Code
byte[] binaryData = File.ReadAllBytes("document.pdf");
string base64Data = Convert.ToBase64String(binaryData);

// Create QR code with high error correction for binary data
GeneratedBarcode binaryBarcode = QRCodeWriter.CreateQrCode(
    base64Data,
    errorCorrectionLevel: QRCodeWriter.QrErrorCorrectionLevel.High
);

// Save with appropriate size for data density
binaryBarcode.ResizeTo(500, 500);
binaryBarcode.SaveAsPng("binary-data-qr.png");
C#

How Do I Create Barcode From Memory Stream?

When Should I Use Memory Streams for Barcode Generation?

Memory streams work best when processing data that doesn't require disk storage, such as dynamically generated content in web applications or database processing. The Export Barcode as Stream guide provides additional context for stream-based workflows.

The following code demonstrates generating a barcode from a memory stream:

using IronBarCode;
using System.IO;
using System.Text;

MemoryStream text = new MemoryStream(Encoding.UTF8.GetBytes("Hello, World!"));
MemoryStream url = new MemoryStream(Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/barcode/"));
MemoryStream receiptID = new MemoryStream(Encoding.UTF8.GetBytes("2023-08-04-12345")); // Receipt ID (numeric id)
MemoryStream flightID = new MemoryStream(Encoding.UTF8.GetBytes("FLT2023NYC-LAX123456")); // Flight id (alphanumeric id)
MemoryStream number = new MemoryStream(Encoding.UTF8.GetBytes("1234"));

BarcodeWriter.CreateBarcode(text, BarcodeEncoding.Aztec).SaveAsPng("text.png");
BarcodeWriter.CreateBarcode(url, BarcodeEncoding.QRCode).SaveAsPng("url.png");
BarcodeWriter.CreateBarcode(receiptID, BarcodeEncoding.Code93, 250, 67).SaveAsPng("receiptID.png");
BarcodeWriter.CreateBarcode(flightID, BarcodeEncoding.PDF417, 250, 67).SaveAsPng("flightID.png");
BarcodeWriter.CreateBarcode(number, BarcodeEncoding.Codabar, 250, 67).SaveAsPng("number.png");

What Are the Benefits of Using Memory Streams?

This snippet creates a MemoryStream from a System.Byte[] object, then uses it as input in BarcodeWriter.CreateBarcode() to generate a barcode from MemoryStream data. Memory streams offer several advantages:

  1. Performance: No disk I/O operations, faster for temporary data
  2. Security: Data remains in memory, reducing sensitive information exposure
  3. Flexibility: Easy integration with stream-based APIs and libraries
  4. Resource Efficiency: Automatic memory management and disposal

Advanced Stream Processing Example

For complex scenarios involving stream processing, combine IronBarcode with other streaming operations:

using IronBarCode;
using System.IO;
using System.Text;

// Example: Processing multiple barcodes in a batch using streams
public static List<Stream> GenerateBarcodeStreams(List<string> dataItems)
{
    var barcodeStreams = new List<Stream>();
    
    foreach (var item in dataItems)
    {
        // Convert string to stream
        var dataStream = new MemoryStream(Encoding.UTF8.GetBytes(item));
        
        // Generate barcode from stream
        var barcode = BarcodeWriter.CreateBarcode(dataStream, BarcodeEncoding.Code128);
        
        // Export barcode back to stream
        var outputStream = barcode.ToStream();
        outputStream.Position = 0; // Reset position for reading
        
        barcodeStreams.Add(outputStream);
    }
    
    return barcodeStreams;
}

// Usage example
var orderNumbers = new List<string> { "ORD-001", "ORD-002", "ORD-003" };
var barcodes = GenerateBarcodeStreams(orderNumbers);
C#

For asynchronous operations and improved performance in multi-threaded applications, see the Use Async and Multithread guide.

Frequently Asked Questions

How do I create a barcode from a string in C# using IronBarcode?

You can create a barcode from a string using the IronBarcode's `CreateBarcode()` method. For example, to generate a Code128 barcode from a simple string, use `var barcode = IronBarCode.BarcodeWriter.CreateBarcode("Order123", IronBarCode.BarcodeWriterEncoding.Code128);`.

What are the different barcode formats supported by IronBarcode for string data?

IronBarcode supports various barcode formats for string data such as QR Codes, Code128, PDF417, Code93, and Aztec. Each format is optimized for different data types, e.g., QR Codes for URLs and large text, Code128 for alphanumeric data, and PDF417 for complex data like flight tickets.

Can I customize the appearance of generated barcodes with IronBarcode?

Yes, IronBarcode allows customization of barcodes. You can resize, change colors, set margins, and add text annotations using methods like `ChangeBarCodeColor()` and `AddAnnotationTextAboveBarcode()`.

Why is character encoding important when creating barcodes from byte arrays in IronBarcode?

Character encoding is crucial because different barcode types require specific encoding formats. IronBarcode supports encodings like ASCII, UTF-8, UTF-16, and ISO-8859-1, ensuring that the input data is correctly encoded for barcode generation.

How can I create a barcode from a byte array using IronBarcode?

To create a barcode from a byte array, convert your string to a byte array in the desired encoding, then use `BarcodeWriter.CreateBarcode()` with the byte array as input. Optionally, set dimensions using parameters like `MaxWidth` and `MaxHeight`.

When should I use memory streams for barcode generation in IronBarcode?

Memory streams are ideal when dealing with data that doesn't need disk storage, such as dynamically generated web content or when processing data from a database. This approach enhances performance and security by keeping data in memory.

What are the benefits of using memory streams for barcode generation?

Memory streams provide faster processing by avoiding disk I/O, enhance security by keeping sensitive data in memory, offer flexibility with stream-based libraries, and ensure efficient resource management with automatic disposal.

How can I export a created barcode as an image file in IronBarcode?

After generating a barcode in IronBarcode, you can export it as an image file using methods like `SaveAsPng()` or `SaveAsJpeg()`, specifying the desired path and file format.

What example use cases benefit from using IronBarcode's PDF417 format?

PDF417 is suitable for complex data storage like flight tickets, government IDs, and other applications requiring robust data encoding. It can store up to 1,850 alphanumeric characters.

How can IronBarcode handle binary data for QR code generation?

IronBarcode can handle binary data by converting it to a Base64 string and then generating a QR code with high error correction. This approach is useful for encoding small file data or binary data streams.

Ready to Get Started?

Nuget Downloads 2,422,100Version:2026.9just released

Get your FREE

30-day Trial Key instantly.

bullet_checkedNo credit card or account creation required
bullet_testTest in production
without watermarks
bullet_calendar30 days fully
functional product
bullet_support24/5 technical
support during trial
Get your free 30-day Trial Key instantly.
No credit card or account creation required
C# NuGet Library for PDF
Install with NuGet

Version: 2026.9

PM > Install-Package BarCode
nuget.org/packages/BarCode/
  1. In Solution Explorer, right-click References, Manage NuGet Packages
  2. Select Browse and search "IronBarCode"
  3. Select the package and install
C# PDF DLL
Download DLL

Version: 2026.9

  1. Download and unzip IronBarCode to a location such as ~/Libs within your Solution directory
  2. In Visual Studio Solution Explorer, right click References. Select Browse, "IronBarCode.dll"

Licenses from $999

Key in blue circle

Get your free 30-day Trial Key instantly.

Your trial license will be sent to your email address

No limitations. 100% unlocked. No credit card.

bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
Book your free Live Demo
Booking Badge

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required