Solved

How to generate Barcodes with asp.net

Posted on 2012-04-11
15
1,164 Views
Last Modified: 2012-04-23
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
Comment
Question by:sammySeltzer
15 Comments
 

Assisted Solution

by:koossa
koossa earned 84 total points
ID: 37832915
We're using a .NET control.
http://www.fathsoft.com/bcxnet.html
0
 
LVL 9

Assisted Solution

by:wasiftoor
wasiftoor earned 83 total points
ID: 37832926
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
 
LVL 28

Author Comment

by:sammySeltzer
ID: 37833027
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
 
LVL 7

Assisted Solution

by:micropc1
micropc1 earned 83 total points
ID: 37833215
This example uses a free barcode font from IDAutomation....

http://www.codeproject.com/Articles/30322/Barcodes-in-ASP-NET-applications
0
 
LVL 28

Author Comment

by:sammySeltzer
ID: 37833318
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
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 167 total points
ID: 37835319
Just replace this bit

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

with your code which gets the code from DB.
0
 
LVL 28

Author Comment

by:sammySeltzer
ID: 37835678
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 1

Accepted Solution

by:
VijayBalkawade earned 83 total points
ID: 37836010
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
 
LVL 28

Author Comment

by:sammySeltzer
ID: 37838065
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37839471
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
 
LVL 28

Author Comment

by:sammySeltzer
ID: 37839501
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37839600
Yes. You are probably using a sql statement which only loads one customer or something else similar. Can you show full code?
0
 
LVL 28

Author Comment

by:sammySeltzer
ID: 37839616
    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
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 167 total points
ID: 37839654
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
 
LVL 28

Author Closing Comment

by:sammySeltzer
ID: 37880999
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Round up to 100% in .NET 10 45
VB.NET HttpWebRequest 12 34
Getfiles in vb.net 28 22
Red error squiggly on vb.net 7 12
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now