There are many products out there that can dynamically generate various types of barcodes to be used in your web application, but if you just need something simple that uses the basic Code 3 of 9 symbology this is a simplified way. It utilizes two different .aspx pages. One will create the barcode image, and the other will provide the characters to be translated and embed the barcode page.
The image for the Code 3 of 9 symbology is actually a font. The font has to be installed on the server or computer for the website that will create the barcode, but the browsing user themselves will not have to have the font installed since we will render the barcode as an image in our page.
First, download the free Code 3 of 9 symbology font at from ID Automation. http://www.idautomation.com/fonts/free/
Next, we will create a new .aspx page that will render the barcode image. Let’s call it barcode.aspx. Copy this code in the code-behind:
Partial Class BarCode
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' Get the Requested code to be created.
Dim Code As String = Request("code").ToString()
' Multiply the lenght of the code by 40 (just to have enough width)
Dim w As Integer = Code.Length * 40
' Create a bitmap object of the width that we calculated and height of 100
Dim oBitmap As New Bitmap(w, 100)
' then create a Graphic object for the bitmap we just created.
Dim oGraphics As Graphics = Graphics.FromImage(oBitmap)
' Now create a Font object for the Barcode Font
' (in this case the IDAutomationHC39M) of 18 point size
Dim oFont As New Font("IDAutomationHC39M", 18)
' Let's create the Point and Brushes for the barcode
Dim oPoint As New PointF(2.0F, 2.0F)
Dim oBrushWrite As New SolidBrush(Color.Black)
Dim oBrush As New SolidBrush(Color.White)
' Now lets create the actual barcode image
' with a rectangle filled with white color
oGraphics.FillRectangle(oBrush, 0, 0, w, 100)
' We have to put prefix and sufix of an asterisk (*),
' in order to be a valid barcode
oGraphics.DrawString("*" & Code & "*", oFont, oBrushWrite, oPoint)
' Then we send the Graphics with the actual barcode
Response.ContentType = "image/jpeg"
We place our code inside the Page_Load routine because when the barcode.aspx page gets displayed it should dynamically create the barcode image. The first line take the string that is sent in the form of a request to this page and stores it a the variable ‘Code’. We are going to send this request from our container .aspx page.
Create a second .aspx page that will be used as your container page, call it container.aspx. Add an image control and call it ‘myBarCode’. This will hold the rendered barcode image.
<asp:Image id="myBarCode" runat="server" />
Now in the code-behind for your container.aspx page, add the following:
Dim barCodeStr as String = "1234ABCD"
myBarCode.ImageUrl = "BarCode.aspx?code=" + barCodeStr
In this code, ‘barCodeStr’ is the string variable that will hold whatever set of characters you wish to be rendered as a barcode. The string is then sent as a querystring request to the BarCode.aspx page which creates the barcode image. Then the product of that .jpg image is provided as the ImageUrl to the image control placed on your container page.
Now in our example, we prepopulated the character string that we wanted rendered as a barcode, however, you can give value to this string variable in whatever way you need to in order for your application to work, even through a random character string.