How to Create Barcode From Data in C#

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

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.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronBarcode with NuGet Package Manager

    PM > Install-Package BarCode

  2. Copy and run this code snippet.

    var barcode = IronBarCode.BarcodeWriter.CreateBarcode("Order123", IronBarCode.BarcodeWriterEncoding.Code128);
  3. Deploy 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:

:path=/static-assets/barcode/content-code-examples/how-to/create-barcode-from-string.cs
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");
$vbLabelText   $csharpLabel

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");
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");
$vbLabelText   $csharpLabel

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: α = 0x03B1.
  • UTF-32: Fixed 32-bit sequence per character. Example: α = 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:

:path=/static-assets/barcode/content-code-examples/how-to/create-barcode-from-byte.cs
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");
$vbLabelText   $csharpLabel

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 = BarcodeWriter.CreateBarcode(
    base64Data, 
    BarcodeEncoding.QRCode
);

// Set high error correction for data integrity
binaryBarcode.SetQRCodeErrorCorrection(QRCodeErrorCorrection.High);

// Save with appropriate size for data density
binaryBarcode.ResizeTo(500, 500);
binaryBarcode.SaveAsPng("binary-data-qr.png");
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 = BarcodeWriter.CreateBarcode(
    base64Data, 
    BarcodeEncoding.QRCode
);

// Set high error correction for data integrity
binaryBarcode.SetQRCodeErrorCorrection(QRCodeErrorCorrection.High);

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

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:

:path=/static-assets/barcode/content-code-examples/how-to/create-barcode-from-stream.cs
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");
$vbLabelText   $csharpLabel

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 memory stream 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 = new MemoryStream();
        barcode.SaveAsPng(outputStream);
        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);
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 = new MemoryStream();
        barcode.SaveAsPng(outputStream);
        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);
$vbLabelText   $csharpLabel

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 text in C#?

You can create a barcode from text in C# using IronBarcode with just one line of code: BarcodeWriter.CreateBarcode("YourText", BarcodeWriterEncoding.Code128). IronBarcode supports multiple formats including QR Code, Code128, PDF417, Aztec, and more.

What types of data can I encode into barcodes?

IronBarcode allows you to encode various data types including strings, URLs, IDs, binary data (byte arrays), and memory streams. The library automatically handles the conversion and encoding based on your chosen barcode format.

Which barcode format should I use for URLs?

QR Codes are the best choice for encoding URLs in IronBarcode. QR Codes can store up to 4,296 alphanumeric characters and include error correction capabilities, making them ideal for web addresses and email addresses.

What barcode format is best for order numbers and serial codes?

Code128 is the ideal format for alphanumeric data like order numbers and serial codes when using IronBarcode. It's highly efficient for modern applications and provides excellent data density for mixed character sets.

Can I create barcodes from binary data?

Yes, IronBarcode supports creating barcodes from binary data using System.Byte[] arrays or System.IO.Stream inputs through the CreateBarcode method, allowing you to encode any type of binary information.

What image formats can I save barcodes in?

IronBarcode allows you to save generated barcodes in multiple image formats including PNG, JPEG, BMP, GIF, and TIFF. The library provides flexible export options for different use cases.

How much data can I store in a PDF417 barcode?

PDF417 barcodes created with IronBarcode can store up to 1,850 alphanumeric characters, making them perfect for complex data like flight tickets, shipping labels, and government IDs.

Which barcode format is best for mobile ticketing?

Aztec barcodes are optimal for mobile ticketing and transportation applications when using IronBarcode. They require less space than QR codes while maintaining high data capacity and readability on mobile devices.

Hairil Hasyimi Bin Omar
Software Engineer
Like all great engineers, Hairil is an avid learner. He’s refining his knowledge of C#, Python, and Java, using that knowledge to add value to team members across Iron Software. Hairil joined the Iron Software team from Universiti Teknologi MARA in Malaysia, where he graduated with a Bachelor's degree ...
Read More
Ready to Get Started?
Nuget Downloads 2,015,591 | Version: 2025.12 just released