Free Support Forum - aspose.com

How to embed the barcode into a tiff image with c#?

Sorry if this seems basic, I'm learning C# at the moment. I'm trying to find a way to embed the barcode into an existing tiff image. My goal is to generate alot of test data with unique barcodes embedded into a tiff image. I would like to add a barcode to each tiff image I have. If you have any code examples, that would be great.

Thanks!

Hi,

Aspose.Barcode does not have built-in support for embedding barcode images in existing image files. But you can generate barcode and embed it using Graphics class of .NET. Please see the following code example.

// create a bitmap for the existing image

Bitmap bmp1 = new Bitmap(@"c:\temp\Barcode.jpeg");

// create graphics object for embedding

Graphics graphics = Graphics.FromImage(bmp1);

// generate barcode

BarCodeBuilder objBarCodeBuilder = new BarCodeBuilder();

objBarCodeBuilder.SymbologyType = Aspose.BarCode.Symbology.Code39Standard;

objBarCodeBuilder.CodeText = "abc-123";

Bitmap imgBarcode = objBarCodeBuilder.GenerateBarCodeImage();

System.IO.MemoryStream stream = new System.IO.MemoryStream();

// save barcode in stream

imgBarcode.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);

// create the bitmap for the stream

Bitmap bmp2 = new Bitmap(stream);

// insert the barcode into the existing image

graphics.DrawImage(bmp2, new Rectangle(0, 0, bmp2.Width, bmp2.Height));

graphics.Save();

// save the image

bmp1.Save(@"c:\temp\BarcodeEmbedded.jpeg");

//bmp1.Save(@"c:\temp\Barcode.jpeg");

bmp2.Dispose();

graphics.Dispose();

bmp1.Dispose();