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

by Hairil Hasyimi Bin Omar

IronBarcode's flexibility enables it to generate barcodes from a range of data sources, including Text, URLs, IDs, Numbers, Binary Data, and Memory Streams. This versatility caters to different scenarios, such as creating barcodes for product identification, URL links, IDs for access control, numeric codes for tracking, and even converting binary data or memory streams into scannable barcodes. This makes IronBarcode a powerful tool for enhancing data-driven processes in various industries.

IronBarcode simplifies barcode writing by allowing various object types as input for the BarcodeWriter.CreateBarcode() method, eliminating the need for type casting. This reduces code complexity and boosts productivity.


C# NuGet Library for

Install with NuGet

Install-Package BarCode
or
Java PDF JAR

Download DLL

Download DLL

Manually install into your project

C# NuGet Library for

Install with NuGet

Install-Package BarCode
or
Java PDF JAR

Download DLL

Download DLL

Manually install into your project

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

First Step:
green arrow pointer

Check out IronBarcode on Nuget for quick installation and deployment. With over 8 million downloads, it's transforming with C#.

C# NuGet Library for nuget.org/packages/BarCode/
Install-Package BarCode

Consider installing the IronBarcode DLL directly. Download and manually install it for your project or GAC form: IronBarCode.zip

Dll Img related to Create Barcode from Text, URLs, IDs, Number, Binary Data & Memory Streams

Manually install into your project

Download DLL

Create Barcode From String

IronBarcode readily accepts System.String data as a parameter in BarcodeWriter.CreateBarcode(). This covers various data types like texts, URLs, IDs, and numbers. Add these directly into the method. See code snippet below:

: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#

The provided code snippet demonstrates converting 5 string examples into barcodes: simple text, URL, numeric ID, alphanumeric ID, and number strings. These strings are input directly into BarcodeWriter.CreateBarcode() along with parameters for BarcodeEncoding and optional dimensions. The produced barcode can be saved as Images, Streams, HTML string, or as PDF document. Below are the resulting barcode images from the code snippet executed in IronBarcode.

Text
URL
Receipt ID
Flight ID
Number

Create Barcode From Byte Array

Besides converting System.String objects into barcodes, users often integrate IronBarcode into complex applications, utilizing other objects like System.Byte []. To use this object, ensure byte encodings align with the required BarcodeEncoding. Here are reference examples of byte encodings:

  • 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.
  • Unicode:
    • Definition: Unicode is a character encoding standard that aims to cover all characters and symbols used in human writing systems. It provides a unique code point for each character.
    • Example: The Unicode code point for the Latin letter 'A' is U+0041, for the Greek letter alpha (α) is U+03B1.
  • 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
Default byte encoding in IronBarcode is ISO-8859-1. If users don't specify the byte encoding, this encoding will be used.

Before proceeding
When choosing byte encoding to be used, please consider the barcode encoding to be used as well, since not every barcode encoding can accept input from all byte encoding. Refer this page for more information on the barcode encodings.

Now, let's see the code snippet to demonstrate this:

: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 System.Byte []. 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 Stream

It seems not complete if a barcode API supports byte array inputs, but does not support System.IO.Stream object. Hence, IronBarcode also provide support for that! This is convenient for users dealing with MemoryStreams who want to generate barcodes without converting input types. Here's the code snippet to demonstrate.

: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#

The provided 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 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.