We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Problems saving barcode image to stream


I am using BarCodeBuilder to generate 2D DataMatrix barocde and save it to stream. The barcode image stream then gets embedded into a PDF. The problem is that that when the barcode is saved to the stream, the stream is either corrupted or just invalid and can’t be read by anything. Below is the code:

Dim codeBuilder As New BarCodeBuilder(barCodeData,Aspose.BarCode.Symbology.DataMatrix)
codeBuilder.Resolution = New Resolution(125, 125, ResolutionMode.Graphics)
codeBuilder.CodeLocation = CodeLocation.None
Dim mStream as Stream
codeBuilder.Save(mStream, System.Drawing.Imaging.ImageFormat.Png)

Everything looks pretty simple but try reading the mStream variable using below method. The while loop never executes. If I save the same barcode to an image file, it works fine. If I then read this file as a stream, everything works fine also, but the stream saved by the BarCodeBuilder just doesn’t work. Please advise. Thanks.

public static void CopyStream(Stream input, Stream output)
byte[] buffer = new byte[8 * 1024];
int len;
while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
output.Write(buffer, 0, len);

Hi Mark,

Thank you for your inquiry.

With my reply I have attached a sample console application to create a barcode image and insert it into a PDF file by using Aspose.Pdf product.

Intermediately, I have saved the image into an instance of System.IO.MemoryStream that can also be used to save the image file on disk without any problem.

Please check the source code and feel free to comment.

Regarding your question about saving the image to System.IO.Stream object. We are looking into it to provide you a better solution/answer.


Thanks. I will review the code. I got this to work on my end also. The issue was that when stream is returned from the .Save method, it’s not at position 0 (which it should be and seems like a bug to me). So after calling .Save and saving the Barcode image to a stream, you have to call:

myStream.Position = 0

Then everything works. I’d suggest that this gets resolved in future releases to avoid confusion. Thanks.

Hi Mark,

Thank you for your feedback and valuable suggestion. I will forward your recommendations to our development team.