Barcode, labelling and utility software


Barcodes in ASP.NET

Creating barcode images on ASP.NET pages

There are many possible approaches to generating barcode images for ASP.NET pages. Whatever method is chosen it is important to ensure that the image is created at a higher graphics resolution than the normal 96 dpi used for web images - because the width of the bars is important.

Using a dBarcode.NET component

One method, suitable for use when the barcode is to be data-dependent, is to use two aspx files, one which creates a memorystream barcode image using one of the dLSoft dBarcode.NET components and then uses a dummy ASP.NET page as the SRC for an IMG tag to display the image at the correct size, eg.

<script language="vb" runat="server">
sub Page_Load(Sender As Object, E As EventArgs)
…………………..
m_barcode.Unit = GraphicsUnit.Inch
m_barcode.XUnit = 0
m_barcode.Font=m_Font
m_barcode.Orientation=0
m_barcode.AutoCheckdigit=TRUE
m_barcode.BarcodeHeight = tht     ' desired height in inches
m_barcode.BarcodeWidth = twd    ' desired width in inches
m_barcode.CodeTypeValue = 8
m_barcode.caption = Session("xx")
' this example creates barcodes at 300 pixels per inch
Session("ms")=m_barcode.sBarcode(300,300,System.Drawing.Imaging.ImageFormat.Png)
if Session("ms").Length>0 then
Session("ht")=m_barcode.ImageHeight*96/300 ' adjust size for the desired image resolution
Session("wd")=m_barcode.ImageWidth*96/300
end if
</script>
</head>
<BODY>
<H1>dLSoft dBarcode.NET - on ASP.NET</H1>
<IMG SRC="page2.aspx" HEIGHT=" <% =Session("ht") %> " WIDTH=" <% =Session("wd") %> ">
<p> dBarcode.NET components can generate barcode images in BMP, GIF, PNG, JPG, TIF and WMF formats, and for more than 50 barcode types.
</BODY>
</HTML>

The Session variables ht and wd contain the actual sizes at which the barcode image is to be drawn on the web page. These are calculated when the page is loaded and used when the IMG SRC calls the second page.

The second file (page2.aspx) serves up the image at the correct size, eg.
<%@ LANGUAGE="VB"%>
<%
Response.ContentType="image/png"
Response.BinaryWrite(Session("ms").GetBuffer())
%>

A working sample is included with each dBarcode.NET component distribution, and the technique may be seen in operation at our Barcodes Online service.


Barcode IMG Server for IIS

An alternative approach is to use one of the Barcode IMG Servers for IIS. The Barcode IMG Servers for IIS may be used to include barcode images on ASP.NET pages, and a sample application is included with the distributions.

The principal requirements for generating the images are:

The .apsx page must start with code containing at least (although any language is suitable)

<%@ Page Language="VB" aspcompat=true %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Web" %>

2. The vntStream object (which receives the barcode image) and ht variable (which contains the image height) must be global for the Session, and so must be declared, either in global.asax or global.vb

Dim m_barcode
Dim vntStream
Dim ht As Integer

3. Barcode properties may be set between the tags

<head>
<script language="VB" runat="server" >
if Not IsPostBack Then
    .........
    makebc(Sender, E)
else
    makebc(Sender, E)
End if
End Sub

Sub makebc(Src As Object, E As EventArgs)
Dim m_barcode
m_barcode = Server.CreateObject("B1BARCODE.Control")
m_barcode.CodeType=m
m_barcode.Xunit=12
m_barcode.NominalSize=100
m_barcode.AutoParity=TRUE
.........
set any additional barcode properties required
............
m_barcode.Caption= ...... barcode data
Session("vntStream")=m_barcode.Pic(0)
Session("ht")=(m_barcode.PictureHeight)
</script>
</head>

4. The HTML code displaying the barcode should appear somewhere between <body> tags

<body>
………….
<IMG SRC="barcd0v1.aspx" HEIGHT=" <% =Session("ht") %> ">
………….
</body>

The dummy page barcd0v1.aspx contains the Responses required to transmit the image objects, eg:

<%@ LANGUAGE="VB"%>
<%
Response.ContentType="image/gif"
Response.BinaryWrite(Session("vntStream1"))
Response.End
%>

 


Sites operated by DL Technology Ltd                            barcode software       barcode fonts                  
E-mail: webmaster@dlsoft.com                      [Product index] [Site index
15/06/2008

dLSoft is a registered trade mark of DL Technology Ltd, a company registered in England number 3615271

Award-winning publisher 2006    dLSoft - Award-winning publisher 2006 & 2007     Award-winning publisher 2007