[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1384
  • Last Modified:

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.
0
sammySeltzer
Asked:
sammySeltzer
6 Solutions
 
koossaCommented:
We're using a .NET control.
http://www.fathsoft.com/bcxnet.html
0
 
wasiftoorCommented:
Have you tried the following link. Comes with usable source code:

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=3319&lngWId=10
0
 
sammySeltzerAuthor Commented:
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?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
micropc1Commented:
This example uses a free barcode font from IDAutomation....

http://www.codeproject.com/Articles/30322/Barcodes-in-ASP-NET-applications
0
 
sammySeltzerAuthor Commented:
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.
0
 
CodeCruiserCommented:
Just replace this bit

string Code = Request["code"].ToString();

with your code which gets the code from DB.
0
 
sammySeltzerAuthor Commented:
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?
0
 
VijayBalkawadeCommented:
You can make use of free library, http://qrcodenet.codeplex.com/releases/view/73746

Sample code I used is like this,

QrEncoder qrEncoder = new QrEncoder(Gma.QrCodeNet.Encoding.ErrorCorrectionLevel.H);
QrCode qrCode = qrEncoder.Encode(string to encode);
//Graphics g = this.CreateGraphics();

Renderer renderer = new Renderer(5, Brushes.Black, Brushes.White);

//Save the file on the physical location
string pathToSave = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\BarCodes\\";
if (Directory.Exists(pathToSave))
{
//do nothing
}
else
{
DirectoryInfo di = Directory.CreateDirectory(pathToSave);
}
pathToSave += fields.OrderReqID.ToString() + fields.ShipID + ".png";
renderer.CreateImageFile(qrCode.Matrix, pathToSave, ImageFormat.Png);

Open in new window

0
 
sammySeltzerAuthor Commented:
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,
0
 
CodeCruiserCommented:
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()
0
 
sammySeltzerAuthor Commented:
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.
0
 
CodeCruiserCommented:
Yes. You are probably using a sql statement which only loads one customer or something else similar. Can you show full code?
0
 
sammySeltzerAuthor Commented:
    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

0
 
CodeCruiserCommented:
Its the way you are writing image to response stream. It will only take one image and return to client. You would need to save files somewhere and point client to those files.
0
 
sammySeltzerAuthor Commented:
Thanks all. We decided to abandon this project. I don't know why but thanks for all the help.

Maybe, we can restart later.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now