How to Export Barcodes as Streams

IronBarcode offers the capability to create barcodes and directly convert them to streams, including the use of MemoryStream for further application within your program. This efficient feature eliminates disk I/O overhead, improves performance, reduces storage usage, enhances data security, and provides a more flexible workflow for seamless integration with various applications.


Get started with IronBarcode

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

First Step:
green arrow pointer

Export Barcode as Stream Example

Once you have created the barcode with the desired value, you can utilize the ToStream method to convert the generated barcode into a MemoryStream, with the default format being PNG image. This functionality also works with the QRCodeWriter, even after applying custom styling.

:path=/static-assets/barcode/content-code-examples/how-to/create-barcode-as-stream-to-stream.cs
using IronBarCode;
using System.IO;

// Create one-dimensional barcode
GeneratedBarcode barcode = BarcodeWriter.CreateBarcode("IronBarcode1234", BarcodeEncoding.Code128);

// Convert barcode to stream
Stream barcodeStream = barcode.ToStream();

// Create QR code
GeneratedBarcode qrCode = QRCodeWriter.CreateQrCode("IronBarcode1234");

// Convert QR code to stream
Stream qrCodeStream = qrCode.ToStream();
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Export Barcode as Stream in Various Image Formats

There are multiple methods that a user can use to convert the barcode object into a MemoryStream. These methods simplify the process, allowing users to choose based on the desired image format stream. Below is the list of available methods:

  • BinaryStream property: Returns a System.IO.Stream of the barcode rendered as a Bitmap image.
  • ToGifStream(): For GIF image format.
  • ToJpegStream(): For JPEG/JPG image format.
  • ToPdfStream(): For PDF document format.
  • ToPngStream(): For PNG image format.
  • ToStream(): For PNG image format by default. Users can, however, input the AnyBitmap.ImageFormat enum field as an argument in this method to specify the desired image stream format.
  • ToTiffStream(): For TIFF image format.

Now, let us use the ToJpegStream and ToStream methods to output streams in JPEG image format using the code snippet below:

:path=/static-assets/barcode/content-code-examples/how-to/create-barcode-as-stream-to-jpeg-stream.cs
using IronBarCode;
using IronSoftware.Drawing;
using System.IO;

// Create one-dimensional barcode
GeneratedBarcode barcode = BarcodeWriter.CreateBarcode("IronBarcode1234", BarcodeEncoding.Code128);

// Convert barcode to JPEG stream
Stream barcodeStream = barcode.ToStream(AnyBitmap.ImageFormat.Jpeg);

// Create QR code
GeneratedBarcode qrCode = QRCodeWriter.CreateQrCode("IronBarcode1234");

// Convert QR code to JPEG stream
Stream qrCodeStream = qrCode.ToJpegStream();
Imports IronBarCode
Imports IronSoftware.Drawing
Imports System.IO

' Create one-dimensional barcode
Private barcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("IronBarcode1234", BarcodeEncoding.Code128)

' Convert barcode to JPEG stream
Private barcodeStream As Stream = barcode.ToStream(AnyBitmap.ImageFormat.Jpeg)

' Create QR code
Private qrCode As GeneratedBarcode = QRCodeWriter.CreateQrCode("IronBarcode1234")

' Convert QR code to JPEG stream
Private qrCodeStream As Stream = qrCode.ToJpegStream()
$vbLabelText   $csharpLabel

In summary, IronBarcode makes it incredibly easy and convenient to create and export barcodes to a MemoryStream object, providing a straightforward method for achieving this.

Frequently Asked Questions

What is the benefit of exporting barcodes as streams using a .NET library?

Exporting barcodes as streams with IronBarcode eliminates disk I/O overhead, improves performance, reduces storage usage, enhances data security, and provides a flexible workflow for seamless integration with various applications.

How can I export a barcode to a MemoryStream in C#?

You can use the `ToStream` method from the IronBarcode library to convert a generated barcode into a MemoryStream, with the default format being PNG.

Which image formats can barcodes be exported to using a barcode library?

Barcodes can be exported to several image formats including Bitmap, GIF, JPEG, PDF, PNG, and TIFF using IronBarcode methods like `ToGifStream()`, `ToJpegStream()`, `ToPdfStream()`, `ToPngStream()`, and `ToTiffStream()`.

How do I create a barcode using a .NET library?

You can create a barcode by using the `BarcodeWriter.CreateBarcode` method in IronBarcode and specifying the value and encoding type, such as `BarcodeEncoding.Code128`.

What is the default image format when using a method to convert barcodes to streams?

The default image format is PNG when using the `ToStream()` method in IronBarcode.

Can I specify a different image format when exporting a barcode to a stream?

Yes, you can specify a different image format by using the `AnyBitmap.ImageFormat` enum field as an argument in the `ToStream()` method in IronBarcode.

Is it possible to apply custom styling to a QR code before exporting it as a stream?

Yes, you can apply custom styling to a QR code before exporting it as a stream using IronBarcode.

What is a practical example of using a MemoryStream from a barcode in an application?

A practical example is saving the MemoryStream to a file, such as using `File.WriteAllBytes()` to write the stream data to a PNG or JPEG file.

What .NET library is used for creating and exporting barcodes as streams?

The .NET library used for creating and exporting barcodes as streams is IronBarcode.

How can I start using a .NET library to export barcodes as streams?

To start using IronBarcode, you need to download the C# library, create various barcode types from input values, and then use specific methods to export the barcode as a 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.