Link to home
Start Free TrialLog in
Avatar of sammySeltzer
sammySeltzerFlag for United States of America

asked on

How to generate Barcodes with asp.net

Greetings mates,

Does anyone know how to generate barcodes?

We are trying to come up with a BarCode for one of our apps.

Management would like a barcode to have customer names (First Name, Last Name) so it can be scanned using one of pitney bowes scanning systems.

I don't mind a link but the link should have *working* examples. I have already scowed the web and each example I tried doesn't work.

Thanks a lot in advance.
SOLUTION
Avatar of koossa
koossa

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sammySeltzer

ASKER

Thanks for the response guys.

koossa, your link looks great but we would like a freeware. That's not free.

wasiftoor, have you tried that code?
SOLUTION
Avatar of Chris Ashcraft
Chris Ashcraft
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I saw that. I actually tried to use that.

Now, it works when I hardcode the value that is supposed to be passed from barcode.aspx?code=...

I have 2 questions then.

1, is that supposed to work with the following code?

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" 
   AutoEventWireup="false" Inherits="BarCodes.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>WebForm1</title>
  <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
  <meta name="CODE_LANGUAGE" Content="C#">
  <meta name="vs_defaultClientScript" content="JavaScript">
  <meta name="vs_targetSchema" 
        content="http://schemas.microsoft.com/intellisense/ie5">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <asp:Image id="myBarCode"  runat="server"></asp:Image>
  </form>
 </body>
</HTML>

Open in new window

which is at the bottom of that tutorial page?

Second, I would like the value generated from the db.

My biggest issue with the code is we are running windows application.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I actually tried that and it worked. What I have been trying to figure out is:

A, how I do modify the code a bit to generate all the barcodes for each customer.

I know it will be a huge number of records but it is good to do this.

I tried this:

        While dreader.Read()
            FullName = dreader.GetString(0)
            FullName = StrConv(FullName, vbProperCase)
        End While
            dreader.Close()

            '************BarCode Starts***********************
            ' Get the Requested code to be created.
            Dim Code As String = FullName

but kept getting just one record.

B, Perhaps, use search so I can grab one record at a time.

What do you think?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok, I am able to get it to work this way:

Markup:
 <body ms_positioning="GridLayout">
  <form id="Form1" method="post" runat="server">
    <input type="hidden" name="code" value="hqcfqs55kioevn55saz0me55" />  
    <asp:Button ID="Button1" runat="server" Text="Generate barcode"></asp:Button> 
</form> 

Open in new window


CodeBehind:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Page.IsPostBack Then
            Dim code As String = Request.Form("code")
            If Not String.IsNullOrEmpty(code) Then
                ' Generate your image here, a code has been specified 
            End If

            ' Get the Requested code to be created.

            ' 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"
            oBitmap.Save(Response.OutputStream, ImageFormat.Jpeg)

        End If
    End Sub

Open in new window


However, We want the code or in our case, name, to be generated from the db.

How do I modify this code to make it happen?

Thanks,
Change

        While dreader.Read()
            FullName = dreader.GetString(0)
            FullName = StrConv(FullName, vbProperCase)
        End While
            dreader.Close()

            '************BarCode Starts***********************
            ' Get the Requested code to be created.
            Dim Code As String = FullName


to


        While dreader.Read()
            FullName = dreader.GetString(0)
            FullName = StrConv(FullName, vbProperCase)
            '************BarCode Starts***********************
            ' Get the Requested code to be created.
            Dim Code As String = FullName
            'Rest of code to generate barcode.
        End While
            dreader.Close()
Sorry, I should have mentioned that I tried that earlier but it was still drawing just one barcode (for one customer), the very first one it sees.

I must be doing something wrong.
Yes. You are probably using a sql statement which only loads one customer or something else similar. Can you show full code?
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


            Dim connStr As String = ConfigurationManager.ConnectionStrings("dbCust").ConnectionString
            Dim conn As New SqlConnection(connStr)
            Dim myDS As New DataSet
            Dim FullName As String

            Dim sqlStr As String = ""

            sqlStr = "SELECT top 10 fname +' '+ lname AS FullName FROM tblCustomer ORDER BY lname"
            Dim cmd As New SqlCommand(sqlStr, conn)
            Dim myAd As New SqlDataAdapter(cmd)
            'Response.Write(sqlStr)
            'Response.End()
            conn = New SqlConnection(connStr)
            conn.Open()
            cmd = New SqlCommand(sqlStr, conn)

            Dim dreader = cmd.ExecuteReader()


            While dreader.Read()
                FullName = dreader.GetString(0)
                FullName = StrConv(FullName, vbProperCase)


                 ' Get the Requested code to be created.
                Dim Code As String = FullName

                ' 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"
                oBitmap.Save(Response.OutputStream, ImageFormat.Jpeg)

            End While
            dreader.Close()
    End Sub
End Class

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks all. We decided to abandon this project. I don't know why but thanks for all the help.

Maybe, we can restart later.