Barcodes can appear in web browsers as images created on the server. Server-side images are created on the server and transmitted as graphic files to the browser - just like any other graphic or text.
The best solution is for creating barcodes for ASP.NET or HTML is to create a complete graphic of the barcode and transmit it from memory (ie. not requiring writing to the server's disk). Barcode Server is an application that runs on Microsoft Internet Information Server (IIS) and generates barcode images which may be requested by a variety of applications, including web browsers, Microsoft Office applications, Crystal Reports, etc. – in fact any application that can use a link of the form http://...... As the source of an image.
Another technique is provided by the dBarcode.NET components which provide managed-code components for ASP.NET pages.
Barcode image can be incorporated into web pages in several ways.
For simple HTML pages the links to the server can be placed within
<IMG> tags.
So, for example, the web page image shown below:
is generated entirely from the HTML code:
<HTML> <HEAD> <TITLE></TITLE> </HEAD>
<BODY>
<H1>dLSoft Barcode IIS server</H1>
For barcodes on web pages.
<p>This page uses the references similar to <br>
"IMG
SRC="http://localhost/IISnet/barcode.aspx?CodeType=0&Xunit=16&Data=12345""<br>
to display barcodes on this page.
You can view the actual call by selecting View Source for this
page.</p>
<p><IMG
SRC="http://localhost/IISnet/barcode.aspx?CodeType=8&Xunit=20&Data=1234567890">
<IMG
SRC="http://localhost/IISnet/barcode.aspx?CodeType=86&Xunit=40&Data=www.Conso.com">
<IMG
SRC="http://localhost/IISnet/barcode.aspx?CodeType=12&Xunit=13&BarcodeHeight=22&
Indicators=TRUE&Data=501234567890"></p>
<p>The full source code of the barcode.aspx servlet is included in
the Samples folder of the products installation directory</p>
<p><IMG
SRC="http://localhost/IISnet/barcode.aspx?CodeType=0&Xunit=16&Data=1234">
<IMG
SRC="http://localhost/IISnet/barcode.aspx?CodeType=81&Xunit=40&Data=www.Conso.com">
</p>
dLSoft Barcode IIS server may be licenced for 1D barcodes, 2D
barcodes or both.
</BODY></HTML>
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.
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")
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 online here.
The method described above is the preferred method of generating barcode images in ASP.NET, but it can be more convenient in some cases to use a servlet that returns an image in response to a single HTTP GET call.
The SetParam() and Pic() methods are available within the dBarcode.NET components to facilitate this approach. The following ASP.NET file (barcode.aspx) illustrates this:
<%@ Page Language="VB" aspcompat=true %>
<%@ import Namespace="System.IO" %>
<%@ import Namespace="System.Web" %>
<%@ import Namespace="System.Web.SessionState" %>
<script language="vb" runat="server">
sub Page_Load(Sender As Object, E As EventArgs)
Dim loop1, loop2,res As Integer
Dim values as ArrayList= new ArrayList()
Dim coll As NameValueCollection
Dim arr1(), arr2() As String
Dim m_barcode = New AbcnetLib.Abcnet()
'm_barcode.SerialNo="12345"
coll=Request.QueryString
arr1 = coll.AllKeys
if Not IsPostBack Then
'These are the default values for the barcode parameters
'and may be changed if required
Dim m_Font As New Font("Arial", 10, FontStyle.Regular)
m_barcode.Font=m_font
m_barcode.DefResolution=96
m_barcode.CodeTypeValue = 9 ' code 128
m_barcode.Unit = GraphicsUnit.Millimeter
m_barcode.XUnit = 0
m_barcode.BarcodeHeight = 16
m_barcode.BarcodeWidth = 30
m_barcode.Indicators=FALSE
For loop1 = 0 To arr1.GetUpperBound(0)
arr2 = coll.GetValues(loop1)
m_barcode.setParam(arr1(loop1),arr2(0))
next loop1
Session("ms")=m_barcode.Pic("png",0)
end if
try
Response.ContentType="image/x-Png"
Response.BinaryWrite(Session("ms").GetBuffer())
catch
end try
end sub
</script>
This file may be called from an application or a web page using a link of the form:
http:// server/barcode.aspx?CodeType=9&Unit=in&BarcodeHeight=0.5&Xunit=12 &Data=1234567890
and the call returns the memory stream barcode image.
Note that properties that are likely to remain constant for many barcodes are best set in the body of the servlet, leaving only those properties that will change to be passed in the http call. The values passed in the http call overwrite those set in the servlet body.