FAQ TBarCode .NET
How to Convert an Image to Base64?
How to Convert an Image to Base64?
This can be realized by creating a byte array and then converting it to Base64 encoding:
public Byte[] GenerateBarcodeStream2D(String data)
{
TECIT.TBarCode.Barcode barcode = new TECIT.TBarCode.Barcode();
barcode.BarcodeType = TECIT.TBarCode.BarcodeType.DataMatrix;
barcode.Data = data;
barcode.Dpi = 100;
Size optimalSize = barcode.CalculateOptimalBitmapSize(null, 2, 2);
barcode.BoundingRectangle = new Rectangle(0, 0, optimalSize.Width, optimalSize.Height);
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
Bitmap bitmap = barcode.DrawBitmap();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
/*
* // or save as 24 bit JPG
* ImageCodecInfo jpgCodec = ImageCodecInfo.GetImageEncoders().Where(codec => codec.FormatID.Equals(ImageFormat.Jpeg.Guid)).FirstOrDefault();
* System.Drawing.Imaging.EncoderParameters parameters = new System.Drawing.Imaging.EncoderParameters();
* parameters.Param[0] = new EncoderParameter (System.Drawing.Imaging.Encoder.ColorDepth, 24);
* bitmap.Save(ms, jpgCodec, parameters);
*/
ms.Position = 0;
return ms.ToArray();
}
}
Once you created the Byte array you can convert the array to Base64. For embedding the image into html, prefix the image with the mime type:
Byte[] barcodeImageStream = GenerateBarcodeStream2D ("12345");
String myBase64Barcode = Convert.ToBase64String(barcodeImageStream);
String imgSrc = "data:image/png;base64, " + myBase64Barcode;