How to Export Barcodes as Streams

Hairil Hasyimi Bin Omar
Hairil Hasyimi Bin Omar
July 27, 2023
Updated December 17, 2024
Share:

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 are for simplicity, in which users can use 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 as default. Users can however, can input 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.

Hairil Hasyimi Bin Omar
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.