What is the proper way to handle the following:
private static Stream BuildDataMatrixCode( int data )
{
var imgStream = new MemoryStream();
var barcode = new BarCodeBuilder
{
SymbologyType = Symbology.DataMatrix,
CodeText = data.ToString( CultureInfo.InvariantCulture ),
DataMatrixEncodeMode = DataMatrixEncodeMode.Auto,
CodeLocation = CodeLocation.None,
AutoSize = true,
ImageHeight = 5F,
ImageWidth = 5F
};
barcode.Save( imgStream, ImageFormat.Png );
imgStream.Flush();
return imgStream;
}
@gmitchell.surgicalno
Please find the code sample given below:
private static Stream BuildDataMatrixCode(int data)
{
MemoryStream imgStream = new MemoryStream();
BarCodeBuilder barcode = new BarCodeBuilder();
barcode.CodeText = data.ToString(CultureInfo.InvariantCulture);
barcode.EncodeType = EncodeTypes.DataMatrix;
barcode.DataMatrixEncodeMode = DataMatrixEncodeMode.Auto;
barcode.CodeLocation = CodeLocation.None;
barcode.AutoSize = true;
barcode.ImageHeight = 5F;
barcode.ImageWidth = 5F;
string dataDir = RunExamples.GetDataDir_ManageBarCodes();
// Call save method of BarCodeImage to store Png barcode image to memory stream
barcode.Save(@"D:\" + imgStream + "_out.png", BarCodeImageFormat.Png);
imgStream.Flush();
return imgStream;
}
BarCodeBuilder has been depreciated. So I was looking more to use BarCodeGenerator as suggested.
Here is both versions, however Image height/width and save to stream are wrong syntax:
MemoryStream imgStream = new MemoryStream();
BarCodeBuilder barcode = new BarCodeBuilder();
barcode.CodeText = data.ToString(CultureInfo.InvariantCulture);
barcode.EncodeType = EncodeTypes.DataMatrix;
barcode.DataMatrixEncodeMode = DataMatrixEncodeMode.Auto;
barcode.CodeLocation = CodeLocation.None;
barcode.AutoSize = true;
barcode.ImageHeight = 5F;
barcode.ImageWidth = 5F;
barcode.Save( imgStream, ImageFormat.Png );
using (BarCodeGenerator generator = new BarCodeGenerator(EncodeTypes.DataMatrix))
{
generator.CodeText = data.ToString( CultureInfo.InvariantCulture );
generator.DataMatrix.EncodeMode = DataMatrixEncodeMode.Auto;
generator.CodeTextStyle.Location= CodeLocation.None;
generator.AutoSizeMode = AutoSizeMode.Nearest;
generator.BarCodeHeight = new Unit(5f,GraphicsUnit.Millimeter);
generator.BarCodeWidth = new Unit(5f,GraphicsUnit.Millimeter );
generator.Save(imgStream, ImageFormat.Png);
}
imgStream.Flush();
return imgStream;
@gmitchell.surgicalno
Thank you for writing back to us.
For Image height and width settings using BarCodeGenerator class, you may use the following code for height and width settings in case of generating BarCode with restricted BarCode size
generator.BarCodeWidth.Pixels = 200;
generator.BarCodeHeight.Pixels = 200;
While generating BarCode without restricted size, you may use like as:
generator.XDimension.Pixels = 3;
generator.Margins.Left.Pixels = 20;
generator.Margins.Right.Pixels = 20;
However, unit based height and width settings can be achieved as follows:
generator.BarCodeWidth.Millimeters = 20;
generator.BarCodeHeight.Millimeters = 20;
For saving into stream, you may need to pass BarCodeImageFormat like as:
generator.Save(dataDir + mStream + "_out.png", BarCodeImageFormat.Png);
Moreover, you may find more details at this link:
https://docs.aspose.com/display/barcodenet/Generating+Barcodes+using+New+Barcode+Generation+API
in regards to saving to a stream, I’m still a little lost. Are you suggesting that I have to write to a file, then read the stream opposed to creating a stream directly?
@gmitchell.surgicalno
Sorry for inconvenience.
You may use it simply like this:
generator.Save(mStream, BarCodeImageFormat.Png);
Save method saves BarCodeImage to stream in a specific format provided as parameter in the form of BarCodeImageFormat. In above mentioned code example, generator is saving PNG BarCodeImage stream.