Home > Code > C# > How to Generate Multiple Barcodes on a Single Image inside .NET Apps

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

by sher azam   on Jul 22, 2014   Category: C#   |  Views: 860    |  Points: 25   |  Gold 

This Technical tip explains how .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.

The following code snippet shows how to generate multiple barcodes on a single image

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

Overview: Aspose.BarCode for .NET

Aspose.BarCode is a .NET component for generation and recognition of Linear and 2D barcodes on all kinds of .NET applications. It supports WPF with 29+ Barcode symbologies like OneCode, QR, Aztec, MSI, EAN128, EAN14, SSCC18, Code128, Code39, Postnet, MarcoPDF417, Datamatrix, UPCA etc. Other features include barcode insertion in PDF, Word and Excel documents. 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.

More about Aspose.BarCode for .NET

- Homepage of Aspose.BarCode for .NET: http://www.aspose.com/.net/barcode-component.aspx

- Download of Aspose.BarCode for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.barcode-for-.net/default.aspx

Post Code  |  Code Snippet Home

User Responses

No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts