How to create Barcodes as Streams in C# Using IronBarcode

In this tutorial, we explore the process of creating barcodes as streams in C# using the Iron Barcode library. Assuming Iron Barcode is installed on your machine, we dive straight into the coding process by importing the Iron Barcode namespace in our Program.cs file. The tutorial demonstrates how to create a barcode containing the URL of Iron Barcode using the Data Matrix encoding scheme. Once the barcode is created, it is converted and exported into various formats such as JPEG, PNG, TIFF, GIF, and PDF using the methods provided by Iron Barcode. Additionally, the barcode is converted to a default stream if no specific format is specified. The tutorial concludes by running the program and successfully generating and exporting the barcodes. Users are encouraged to explore the extensive functionalities offered by Iron Barcode and to subscribe for more tutorials or try Iron Software through a trial subscription.

Here's a sample implementation in C#:

// Import the IronBarcode namespace to allow barcode generation
using IronBarCode;

class Program
{
    static void Main(string[] args)
    {
        // Create a Barcode with a Data Matrix encoding scheme
        var barcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.DataMatrix);

        // Export barcode to a JPEG stream
        using (var jpegStream = barcode.ToStream(BarcodeEncoding.JPEG))
        {
            // Stream is ready to be used as needed, e.g., saving to a file or uploading
        }

        // Export barcode to a PNG stream
        using (var pngStream = barcode.ToStream(BarcodeEncoding.PNG))
        {
            // Stream is ready to be used as needed
        }

        // Export barcode to a TIFF stream
        using (var tiffStream = barcode.ToStream(BarcodeEncoding.TIFF))
        {
            // Stream is ready to be used as needed
        }

        // Export barcode to a GIF stream
        using (var gifStream = barcode.ToStream(BarcodeEncoding.GIF))
        {
            // Stream is ready to be used as needed
        }

        // Export barcode to a PDF stream
        using (var pdfStream = barcode.ToStream(BarcodeEncoding.PDF))
        {
            // Stream is ready to be used as needed
        }

        // Handling a case where no specific format is specified
        using (var defaultStream = barcode.ToStream())
        {
            // Default stream handling, e.g., exporting in standard image format
        }
    }
}
// Import the IronBarcode namespace to allow barcode generation
using IronBarCode;

class Program
{
    static void Main(string[] args)
    {
        // Create a Barcode with a Data Matrix encoding scheme
        var barcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.DataMatrix);

        // Export barcode to a JPEG stream
        using (var jpegStream = barcode.ToStream(BarcodeEncoding.JPEG))
        {
            // Stream is ready to be used as needed, e.g., saving to a file or uploading
        }

        // Export barcode to a PNG stream
        using (var pngStream = barcode.ToStream(BarcodeEncoding.PNG))
        {
            // Stream is ready to be used as needed
        }

        // Export barcode to a TIFF stream
        using (var tiffStream = barcode.ToStream(BarcodeEncoding.TIFF))
        {
            // Stream is ready to be used as needed
        }

        // Export barcode to a GIF stream
        using (var gifStream = barcode.ToStream(BarcodeEncoding.GIF))
        {
            // Stream is ready to be used as needed
        }

        // Export barcode to a PDF stream
        using (var pdfStream = barcode.ToStream(BarcodeEncoding.PDF))
        {
            // Stream is ready to be used as needed
        }

        // Handling a case where no specific format is specified
        using (var defaultStream = barcode.ToStream())
        {
            // Default stream handling, e.g., exporting in standard image format
        }
    }
}
' Import the IronBarcode namespace to allow barcode generation
Imports IronBarCode

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Create a Barcode with a Data Matrix encoding scheme
		Dim barcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.DataMatrix)

		' Export barcode to a JPEG stream
		Using jpegStream = barcode.ToStream(BarcodeEncoding.JPEG)
			' Stream is ready to be used as needed, e.g., saving to a file or uploading
		End Using

		' Export barcode to a PNG stream
		Using pngStream = barcode.ToStream(BarcodeEncoding.PNG)
			' Stream is ready to be used as needed
		End Using

		' Export barcode to a TIFF stream
		Using tiffStream = barcode.ToStream(BarcodeEncoding.TIFF)
			' Stream is ready to be used as needed
		End Using

		' Export barcode to a GIF stream
		Using gifStream = barcode.ToStream(BarcodeEncoding.GIF)
			' Stream is ready to be used as needed
		End Using

		' Export barcode to a PDF stream
		Using pdfStream = barcode.ToStream(BarcodeEncoding.PDF)
			' Stream is ready to be used as needed
		End Using

		' Handling a case where no specific format is specified
		Using defaultStream = barcode.ToStream()
			' Default stream handling, e.g., exporting in standard image format
		End Using
	End Sub
End Class
$vbLabelText   $csharpLabel

Further Reading: How to Export Barcodes as Streams

Jordi Bardia
Software Engineer
Jordi is most proficient in Python, C# and C++, when he isn’t leveraging his skills at Iron Software; he’s game programming. Sharing responsibilities for product testing, product development and research, Jordi adds immense value to continual product improvement. The varied experience keeps him challenged and engaged, and he says it’s one of his favorite aspects of working with Iron Software. Jordi grew up in Miami, Florida and studied Computer Science and Statistics at University of Florida.
< PREVIOUS
How to Read & Scan Barcode in C# Using NET MAUI
NEXT >
How to Customize and Add Logos to QR Codes in C#