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
// Import necessary namespaces for barcode generation and file handling
using IronBarCode;
using System.IO;

// The following code demonstrates how to generate a one-dimensional barcode using Code128 encoding,
// and how to generate a QR code, both using the IronBarCode library.

// Generate a one-dimensional barcode using Code128 encoding
GeneratedBarcode barcode = BarcodeWriter.CreateBarcode("IronBarcode1234", BarcodeEncoding.Code128);

// Convert the generated barcode to a Stream object for further processing or saving
Stream barcodeStream = barcode.ToStream();

// At this point, `barcodeStream` can be used to save the barcode to a file,
// send over a network, etc., as it is in a Stream format.

// Generate a QR code from the specified string data
GeneratedBarcode qrCode = QRCodeWriter.CreateQrCode("IronBarcode1234");

// Convert the generated QR code to a Stream object for further processing or saving
Stream qrCodeStream = qrCode.ToStream();

// Similar to `barcodeStream`, `qrCodeStream` is a Stream object containing the QR code,
// which can be used for various purposes such as saving to a file or network transmission.
' Import necessary namespaces for barcode generation and file handling

Imports IronBarCode

Imports System.IO



' The following code demonstrates how to generate a one-dimensional barcode using Code128 encoding,

' and how to generate a QR code, both using the IronBarCode library.



' Generate a one-dimensional barcode using Code128 encoding

Private barcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("IronBarcode1234", BarcodeEncoding.Code128)



' Convert the generated barcode to a Stream object for further processing or saving

Private barcodeStream As Stream = barcode.ToStream()



' At this point, `barcodeStream` can be used to save the barcode to a file,

' send over a network, etc., as it is in a Stream format.



' Generate a QR code from the specified string data

Private qrCode As GeneratedBarcode = QRCodeWriter.CreateQrCode("IronBarcode1234")



' Convert the generated QR code to a Stream object for further processing or saving

Private qrCodeStream As Stream = qrCode.ToStream()



' Similar to `barcodeStream`, `qrCodeStream` is a Stream object containing the QR code,

' which can be used for various purposes such as saving to a file or network transmission.
$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;

// Generate a one-dimensional barcode using Code128 encoding
GeneratedBarcode barcode = BarcodeWriter.CreateBarcode("IronBarcode1234", BarcodeEncoding.Code128);

// Convert the barcode image to a JPEG format stream
// Corrected the usage of the ImageFormat parameter to match IronBarCode library conventions. 
Stream barcodeStream = barcode.ToStream(IronSoftware.Drawing.ImageFormat.Jpeg);

// Generate a QR code using the specified information
GeneratedBarcode qrCode = QRCodeWriter.CreateQrCode("IronBarcode1234");

// Convert the QR code image to a JPEG format stream
// Corrected the usage of the ImageFormat parameter to match IronBarCode library conventions.
Stream qrCodeStream = qrCode.ToStream(IronSoftware.Drawing.ImageFormat.Jpeg);
Imports IronBarCode

Imports IronSoftware.Drawing

Imports System.IO



' Generate a one-dimensional barcode using Code128 encoding

Private barcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("IronBarcode1234", BarcodeEncoding.Code128)



' Convert the barcode image to a JPEG format stream

' Corrected the usage of the ImageFormat parameter to match IronBarCode library conventions. 

Private barcodeStream As Stream = barcode.ToStream(IronSoftware.Drawing.ImageFormat.Jpeg)



' Generate a QR code using the specified information

Private qrCode As GeneratedBarcode = QRCodeWriter.CreateQrCode("IronBarcode1234")



' Convert the QR code image to a JPEG format stream

' Corrected the usage of the ImageFormat parameter to match IronBarCode library conventions.

Private qrCodeStream As Stream = qrCode.ToStream(IronSoftware.Drawing.ImageFormat.Jpeg)
$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
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.