Create Barcode from Text, URLs, IDs, Numbers, Binary Data & Memory Streams

by Hairil Hasyimi Bin Omar

Barcodes can be generated from either strings, binary data, or memory streams. All of these data formats can be used as input in the BarcodeWriter.CreateBarcode() method.


Get started with IronBarcode

Start using IronBarcode in your project today with a free trial.

First Step:
green arrow pointer

Create Barcode From String

The following code snippet 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");
Imports IronBarCode

Private text As String = "Hello, World!"
Private url As String = "https://ironsoftware.com/csharp/barcode/"
Private receiptID As String = "2023-08-04-12345" ' Receipt ID (numeric id)
Private flightID As String = "FLT2023NYC-LAX123456" ' Flight ID (alphanumeric id)
Private number As String = "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")
VB   C#

In this code snippet, we encode 5 different examples of data into 5 different barcode types: simple text to Aztec, URL to QR Code, a numeric ID to Code 93, an alphanumeric ID to PDF417, and a number to Codabar. The images are then saved as PNG. Below are the resulting images from the code snippet above:

Text
URL
Receipt ID
Flight ID
Number

Create Barcode From Byte Array

To create barcodes from data from a byte array, first ensure the character encoding aligns with the required BarcodeEncoding, since each barcode type accepts a different character encoding. Here are the different character encodings that can be used in IronBarcode:

  • ASCII (American Standard Code for Information Interchange):
    • Definition: This encoding uses 7 bits to represent each character, including English letters, digits, punctuation, and control characters.
    • Example: The ASCII code for the letter 'A' is 65, 'B' is 66, and so on.
  • UTF-8 (Unicode Transformation Format 8-bit):
    • Definition: UTF-8 is a variable-length character encoding that can represent all Unicode characters. It uses 8-bit sequences to encode characters, and the number of bytes used depends on the character.
    • Example: The UTF-8 encoding of the letter 'A' is the same as its ASCII code point, which is 65. However, for characters outside the ASCII range, UTF-8 uses multiple bytes. For example, the UTF-8 encoding of the Euro symbol (€) is the sequence 0xE2 0x82 0xAC.
  • UTF-16 (Unicode Transformation Format 16-bit):
    • Definition: UTF-16 is another variable-length character encoding that uses 16-bit sequences to encode characters. It's capable of representing the entire Unicode character set.
    • Example: The UTF-16 encoding of the Latin letter 'A' is 0x0041, and the UTF-16 encoding of the Greek letter alpha (α) is 0x03B1.
  • UTF-32 (Unicode Transformation Format 32-bit):
    • Definition: UTF-32 uses a fixed 32-bit sequence for each character, making it straightforward but potentially wasteful in terms of storage space for characters outside the ASCII range.
    • Example: The UTF-32 encoding of the Latin letter 'A' is 0x00000041, and the UTF-32 encoding of the Greek letter alpha (α) is 0x000003B1.
  • ISO-8859-1 (Latin-1):
    • Definition: ISO-8859-1 is a character encoding that extends ASCII to include characters from Western European languages. It uses 8 bits for each character.
    • Example: The ISO-8859-1 code for the letter 'A' is still 65, 'B' is 66, but it also includes characters like 'é' (233) and 'ü' (252).

Please note
The default character encoding in IronBarcode is ISO-8859-1. If users don't specify the character encoding, this encoding will be used.

The following code snippet 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");
Imports IronBarCode
Imports System.Text

Private text() As Byte = Encoding.UTF8.GetBytes("Hello, World!")
Private url() As Byte = Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/barcode/")
Private receiptID() As Byte = Encoding.UTF8.GetBytes("2023-08-04-12345") ' Receipt ID (numeric id)
Private flightID() As Byte = Encoding.UTF8.GetBytes("FLT2023NYC-LAX123456") ' Flight id (alphanumeric id)
Private number() As Byte = 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")
VB   C#

In the given snippet, 5 string inputs are transformed into a System.Byte[] object. To convert these byte arrays into barcodes, include them as parameters in BarcodeWriter, along with the desired BarcodeEncoding. Optionally, set MaxWidth and MaxHeight for barcode size.

Create Barcode From Memory Stream

The following code snippet demonstrates how to generate 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");
Imports IronBarCode
Imports System.IO
Imports System.Text

Private text As New MemoryStream(Encoding.UTF8.GetBytes("Hello, World!"))
Private url As New MemoryStream(Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/barcode/"))
Private receiptID As New MemoryStream(Encoding.UTF8.GetBytes("2023-08-04-12345")) ' Receipt ID (numeric id)
Private flightID As New MemoryStream(Encoding.UTF8.GetBytes("FLT2023NYC-LAX123456")) ' Flight id (alphanumeric id)
Private number As 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")
VB   C#

This code snippet extends upon the previous one. Here, we create new MemoryStream instances from the earlier System.Byte[] objects. As before, users can input these new instances into BarcodeWriter.CreateBarcode() to generate barcodes from MemoryStream objects.

Hairil related to Create Barcode From Memory Stream

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 in Chemical and Process Engineering.