Generating Barcodes with Barcode Generator and ASP.NET

Generating barcodes in ASP.NET is pretty easy with Barcode Generator. Barcode Generator is free for non-commercial use but priced right for commercial use. If you are a commercial site, try the free version locally and, if it works for you, spring for the license. VB.NET is used below but Barcode Generator’s examples are in C#.

This post modifies the example provided by Barcode Generator only enough so you can put a barcode on a page with a plain img tag as such:

<img src="barcode.aspx?code=hqcfqs55kioevn55saz0me55" alt="barcode" />

barcode example from screenshot
I also got some help from this post by Chris Love: A Quick and Dirty Bar Code Image httpHandler

First, follow the instructions on Barcode Generator’s site to install the dll’s.

Then on barcode.aspx put the following:

Imports System.Web
Imports System.Drawing
Imports System.Drawing.Imaging
Imports BarcodeGenerator

Public Class barcode
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim font As New BCGFont(New Font("Arial", 10, FontStyle.Regular))
        Dim barcode As String

        Dim color_black As New BCGColor(Color.Black)
        Dim color_white As New BCGColor(Color.White)

        If Not IsNothing(Request("code")) AndAlso Len(Request("code").ToString) Then
            barcode = Request("code").ToString
            barcode = Now.Month.ToString & Now.Day.ToString & Now.Year.ToString
        End If

        Dim code As BCGBarcode1D = New BCGcode39()
        ' Resolution
        ' Thickness
        ' Color of bars
        ' Color of spaces
        ' Font

        Dim drawing As New BCGDrawing(color_white)

        Response.ContentType = "image/jpeg"
        ' Save the image into jpeg format.
        drawing.finish(ImageFormat.Jpeg, Response.OutputStream)
    End Sub
End Class

And, that’s it. Set your image tag to the path of your barcode.aspx file with the code as a url param and you’re done.

Posted in .net, Web development. Tags: . Permalink. Both comments and trackbacks are closed.