How to Export Barcodes as Streams

by Hairil Hasyimi Bin Omar

C# NuGet Library for

Install with NuGet

Install-Package BarCode
or
C#  DLL

Download DLL

Download DLL

Manually install into your project

Barcodes produced from IronBarcode does not necessarily need to be saved into disk. Being a good API it is, barcodes produced from IronBarcode can take up many types, including MemoryStream to be used further in a program or application.

Create barcode

Barcodes can first be written in IronBarcode by calling CreateBarcode() method from BarcodeWriter class. To write QR codes on the other hand, call CreateQrCode() method from QRCodeWriter class to produce a QR code. More info on how to write/create a barcode, please refer to this article. The code snippet below demonstrates on how to create a simple one-dimensional barcode and a QR code.

:path=/static-assets/barcode/content-code-examples/how-to/CreateBarcode.cs 
:path=/static-assets/barcode/content-code-examples/how-to/CreateBarcode.cs 
'INSTANT VB TODO TASK: The following line uses invalid syntax:
':path=/static-assets/barcode/content-code-examples/how-@to/CreateBarcode.cs
VB   C#

Convert Barcode to Stream

There are multiple methods that a user can use to convert the GeneratedBarcode into MemoryStream. These methods are for simplicity, in which users can use based on the desired image format stream. Below are the list of methods involved:

  • ToGifStream() : This method converts the GeneratedBarcode into System.IO.MemoryStream type for GIF image.
  • ToJpegStream() : This method converts the GeneratedBarcode into System.IO.MemoryStream type for JPEG/JPG image.
  • ToPdfStream() : This method returns System.IO.MemoryStream that contains 1 page of PDF document containing the GeneratedBarcode.
  • ToPngStream() : This method converts the GeneratedBarcode into System.IO.MemoryStream type for PNG image.
  • ToStream() : This method converts the GeneratedBarcode into System.IO.MemoryStream type object of PNG image format as default. Users can however, can input AnyBitmap.ImageFormat enum field as argument in this method to specify the desired image stream format.
  • ToTiffStream() : This method returns System.IO.MemoryStream type object from the GeneratedBarcode with TIFF image format.

Now let us apply one of the methods to convert GeneratedBarcode into System.IO.MemoryStream object using the code snippet below:

:path=/static-assets/barcode/content-code-examples/how-to/ConvertToMemoryStream.cs 
:path=/static-assets/barcode/content-code-examples/how-to/ConvertToMemoryStream.cs 
'INSTANT VB TODO TASK: The following line uses invalid syntax:
':path=/static-assets/barcode/content-code-examples/how-@to/ConvertToMemoryStream.cs
VB   C#

From the code snippet above, users are able to convert a barcode by either attaching the stream conversion method on the GeneratedBarcode object or attach the method inline with BarcodeWriter.CreateBarcode method.

In a nutshell, it is very easy and convenient to use IronBarcode to create and export barcodes to MemoryStream type object by offering a straightforward method. Try it!