Generate Multiple Barcodes within Single Image
Contents
[
Hide
]
Aspose.BarCode for .NET allows creating multiple barcodes of different types within one image, including postal, QR Code, PDF417, EAN, Code 39, Code 128, ISBN, MSI, and others.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_TechnicalArticles(); | |
Dictionary<string, BaseEncodeType> collection = new Dictionary<string, BaseEncodeType>(); | |
collection.Add("ONE123", EncodeTypes.Code39Standard); | |
collection.Add("Process Collection", EncodeTypes.DataMatrix); | |
collection.Add("Dictionary Collection", EncodeTypes.QR); | |
collection.Add("X06712AT", EncodeTypes.Code128); | |
collection.Add("979026000043", EncodeTypes.EAN13); | |
collection.Add("Aztec BarCode", EncodeTypes.Aztec); | |
List<Bitmap> images = new List<Bitmap>(); | |
foreach (KeyValuePair<string, BaseEncodeType> pair in collection) | |
{ | |
BarcodeGenerator builder = new BarcodeGenerator(pair.Value, pair.Key); | |
images.Add(builder.GenerateBarCodeImage()); | |
} | |
int maxWidth = int.MinValue; | |
int sumHeight = 0; | |
foreach (Bitmap bmp in images) | |
{ | |
sumHeight += bmp.Height; | |
if (maxWidth < bmp.Width) | |
maxWidth = bmp.Width; | |
} | |
const int offset = 10; | |
Bitmap resultBitmap = new Bitmap(maxWidth + offset * 2, sumHeight + offset * images.Count); | |
using (Graphics g = Graphics.FromImage(resultBitmap)) | |
{ | |
g.Clear(Color.White); | |
int yPosition = offset; | |
for (int i = 0; i < images.Count; ++i) | |
{ | |
Bitmap currentBitmap = images[i]; | |
g.DrawImage(currentBitmap, offset, yPosition); | |
yPosition += currentBitmap.Height + offset; | |
} | |
} | |
resultBitmap.Save(dataDir + "barcodes_out.png", ImageFormat.Png); |
A sample image with several barcodes is provided below.