DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

David has posted 111 posts at DZone. View Full User Profile

How to Generate Multiple Barcodes on a Single Image inside .NET Apps

  • submit to reddit
//[C# Code Sample]

Dictionary<string, Symbology> collection = new Dictionary<string, Symbology>();
collection.Add("ONE123", Symbology.Code39Standard);
collection.Add("Process Collection", Symbology.DataMatrix);
collection.Add("Dictionary Collection", Symbology.QR);
collection.Add("X06712AT", Symbology.Code128);
collection.Add("979026000043", Symbology.EAN13);
collection.Add("Aztec BarCode", Symbology.Aztec);

List<Bitmap> images = new List<Bitmap>();

foreach (KeyValuePair<string, Symbology> pair in collection)
using (BarCodeBuilder builder = new BarCodeBuilder())
    builder.CodeText = pair.Key;
    builder.SymbologyType = pair.Value;

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))

    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("barcode.png", ImageFormat.Png);

//[VB.NET Code Sample]

Dim collection As New Dictionary(Of String, Symbology)()
collection.Add("ONE123", Symbology.Code39Standard)
collection.Add("Process Collection", Symbology.DataMatrix)
collection.Add("Dictionary Collection", Symbology.QR)
collection.Add("X06712AT", Symbology.Code128)
collection.Add("979026000043", Symbology.EAN13)
collection.Add("Aztec BarCode", Symbology.Aztec)

Dim images As New List(Of Bitmap)()

For Each pair As KeyValuePair(Of String, Symbology) In collection
    Using builder As New BarCodeBuilder()
	builder.CodeText = pair.Key
	builder.SymbologyType = pair.Value
    End Using

Dim maxWidth As Integer = Integer.MinValue
Dim sumHeight As Integer = 0
For Each bmp As Bitmap In images
    sumHeight += bmp.Height
    If maxWidth < bmp.Width Then
	maxWidth = bmp.Width
    End If

Const  offset As Integer = 10
Dim resultBitmap As New Bitmap(maxWidth + offset * 2, sumHeight + offset * images.Count)
Using g As Graphics = Graphics.FromImage(resultBitmap)

    Dim yPosition As Integer = offset
    For i As Integer = 0 To images.Count - 1
  	Dim currentBitmap As Bitmap = images(i)
	g.DrawImage(currentBitmap, offset, yPosition)
	yPosition += currentBitmap.Height + offset
End Using

resultBitmap.Save("barcode.png", ImageFormat.Png)

This Technical tip explains how to .NET developers can generate multiple barcodes on a single image inside their applications using Aspose.BarCode for .NET.   Aspose.BarCode for .NET can efficiently create multiple optimized barcodes on a single image. It also supports generating multiple type of barcodes such as postal, QR, PDF417, EAN, Code 39, Code128, ISBN, MSI, GS1 etc. 

Also take image output in BMP, GIF, JPEG, PNG and WMF formats. You can also control image styles such as background color, bar color etc.


Gordon Steven replied on Thu, 2014/08/21 - 6:57am

 Now this is something that I need. C Frederick Wehba