How to merge images and barcodes in ASP.NET with VB

In web pages, the barcodes are generally displayed as images which are generated at server side by using technologies like ASP.NET, PHP, etc.

There’re situations where the barcode image you want to display on a web page needs to be “watermarked” or identified with your company or organization logo, so it cannot be used for other scenarios. Our Barcode Professional for ASP.NET can be used in ASP.NET web pages for displaying linear, postal and 2D barcodes; and can be merged with other images by using .NET drawing classes.

For example, recently a tech member from GS1 Mexico asked us to do that while evaluating our product. The support team wrote a .NET code in VB (which can be easily converted to C#) which take the GS1 Mexico logo and merge it with a GS1-128 (formerly EAN/UCC-128) barcode image generated by our product to render one image file only. All was implemented into an ASP.NET “General Handler” *.ashx file. Here is the sample code:

Imports System
Imports System.Web

Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO

Imports Neodynamic.WebControls.BarcodeProfessional

Public Class GS1MexBarcodeGen : Implements IHttpHandler
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        'Gets the value to encode from QueryString param
        Dim valToEncode As String = context.Request("code")
        Dim texto As String = context.Request("text")
        'gen barcode image using Barcode Professional for ASP.NET
        Dim imgBarcode As Bitmap
        If String.IsNullOrEmpty(valToEncode) = False Then
            Using bcp As New BarcodeProfessional
                bcp.BarcodeUnit = BarcodeUnit.Inch
                bcp.Symbology = Symbology.GS1128
                bcp.Code = valToEncode
                bcp.BarHeight = 0.52
                bcp.BarWidth = 0.01042
                bcp.QuietZoneWidth = 0.3
                imgBarcode = bcp.GetBarcodeImage()
            End Using
        End If
        'Gen the output image merging the GS1 Mex logo + barcode 
        'IMPORTANT: the gs1mex_logo.jpg image must be located under root's images folder
        Dim imgGS1MexLogo As Bitmap = DirectCast(Image.FromFile(context.Server.MapPath("~/images/gs1mex_logo.jpg")), Bitmap)
        Dim szGS1MexLogo As Size = imgGS1MexLogo.Size
        Dim szFinal As Size = szGS1MexLogo
        If Not imgBarcode Is Nothing Then
            Dim szBarcode As Size = imgBarcode.Size
            szFinal.Height = Math.Max(szFinal.Height, szBarcode.Height)
            szFinal.Width += szBarcode.Width
        End If
        Using imgFinal As New Bitmap(szFinal.Width, szFinal.Height)
            Using gfx As Graphics = Graphics.FromImage(imgFinal)
                'draw GS1 Mex logo
                gfx.DrawImage(imgGS1MexLogo, 0, 0, New Rectangle(0, 0, szGS1MexLogo.Width, szGS1MexLogo.Height), GraphicsUnit.Pixel)
                'draw barcode 
                If Not imgBarcode Is Nothing Then
                    Using imgBarcode
                        gfx.DrawImage(imgBarcode, New Point(szGS1MexLogo.Width, 0))
                    End Using
                End If
            End Using
            Using ms As New MemoryStream()
                imgFinal.Save(ms, ImageFormat.Png)
                ms.Position = 0
                context.Response.ContentType = "image/png"
            End Using
        End Using
    End Sub
    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
            Return False
        End Get
    End Property

End Class

You can display the output image generated by this code by using a simple IMG HTML tag with the correct params, for instance:

<img src="GS1MexBarcodeGen.ashx?code=(00)350123451234567890" alt="GS1-128 barcode" style="border: 5px solid #0055ff" />

The GS1 logo merged with a GS1-128 barcode in ASP.NET

kick it on


One thought on “How to merge images and barcodes in ASP.NET with VB

  1. Pingback:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.