Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

FREE 12 month online training for ASP.NET & MS Expression Studio and a Free copy of MS Expression Web with Windows Server Purchase
How we can Create Bar Code Generator Using Vb.Net.

Author: Zunnair
Download Source Code : 742_Sample1.zip

In this Article I will explain you the way in which we can Create Barcode Generator using VB.net. I implement Bar Code generator Using two standards in this article which we can say

I implement these task in the Sequence Given Below.

1-EAN 2 Bin       : Convert an EAN Code on its binary form   

2-PrintEANBarCode: Print an EAN Barcode on a Picture Box     

Objective      : Convert an EAN8 or EAN13 in a sequence of 1 and 0 where 1 means a stripe and 0 means a space

 

Write the function below this function basically Converts decimal value to binary and also implement The Ean8 And Ean 13 algo or we can say that Force the flow of Process in such a way that they make EAN8 And EAN13 Barcode Generator.

This function first of all Check to see what kind of EAN the code will be and If it's not 8 nor 13 "Error: Invalid Function Call"

Also have check of Check for numbers only otherwise it generate exception such as Invalid char "Error: Invalid Function Call"

 

  

The EAN13 is a special code formated as follow CnnnnnnnnnnnK

Where : C - is not part of the code, but coded in the next 6 digits

n - THE NUMBER K - is a check digit (this function do not calculate this) When the first digit of an EAN13 code is 0 (zero) it matches an UPC (12 digits) code Skip the first digit of the code.

Select the proper code based on the first digit. The EAN BarCode also finish with a "guardian".

Private Function EAN2Bin(ByVal strEANCode As String) As String

        Dim K As Integer

        Dim strAux As String

        Dim strExit As String

        Dim strCode As String

 

        strEANCode = Trim(strEANCode)

        strAux = strEANCode

                If (strAux.Length <> 13) And (strAux.Length <> 8) Then

            Err.Raise(5, "EAN2Bin", "Invalid EAN Code")

        End If

               For K = 0 To strEANCode.Length - 1

            Select Case (strAux.Chars(K).ToString)

                Case Is < "0", Is > "9"

                                        Err.Raise(5, "EAN2Bin", "Invalid char on EAN Code")

            End Select

        Next

        If (strAux.Length = 13) Then

            strAux = Mid(strAux, 2)

                        Select Case CInt(Left(strEANCode, 1))

                Case 0

                    strCode = "000000"

                Case 1

                    strCode = "001011"

                Case 2

                    strCode = "001101"

                Case 3

                    strCode = "001110"

                Case 4

                    strCode = "010011"

                Case 5

                    strCode = "011001"

                Case 6

                    strCode = "011100"

                Case 7

                    strCode = "010101"

                Case 8

                    strCode = "010110"

                Case 9

                    strCode = "011010"

            End Select

        Else

            strCode = "0000"

        End If

        '* The EAN BarCode starts with a "guardian"

        strExit = "000101"

        '* First half of the code

        For K = 1 To Len(strAux) \ 2

            Select Case CInt(Mid(strAux, K, 1))

                Case 0

                    strExit &= IIf(Mid(strCode, K, 1) = "0", "0001101", "0100111")

                Case 1

                    strExit &= IIf(Mid(strCode, K, 1) = "0", "0011001", "0110011")

                Case 2

                    strExit &= IIf(Mid(strCode, K, 1) = "0", "0010011", "0011011")

                Case 3

                    strExit &= IIf(Mid(strCode, K, 1) = "0", "0111101", "0100001")

                Case 4

                    strExit &= IIf(Mid(strCode, K, 1) = "0", "0100011", "0011101")

                Case 5

                    strExit &= IIf(Mid(strCode, K, 1) = "0", "0110001", "0111001")

                Case 6

                    strExit &= IIf(Mid(strCode, K, 1) = "0", "0101111", "0000101")

                Case 7

                    strExit &= IIf(Mid(strCode, K, 1) = "0", "0111011", "0010001")

                Case 8

                    strExit &= IIf(Mid(strCode, K, 1) = "0", "0110111", "0001001")

                Case 9

                    strExit &= IIf(Mid(strCode, K, 1) = "0", "0001011", "0010111")

            End Select

        Next K

        '* Middle "guardian" separator

        strExit &= "01010"

        '* Second half of the code

        For K = Len(strAux) \ 2 + 1 To Len(strAux)

            Select Case CInt(Mid(strAux, K, 1))

                Case 0

                    strExit &= "1110010"

                Case 1

                    strExit &= "1100110"

                Case 2

                    strExit &= "1101100"

                Case 3

                    strExit &= "1000010"

                Case 4

                    strExit &= "1011100"

                Case 5

                    strExit &= "1001110"

                Case 6

                    strExit &= "1010000"

                Case 7

                    strExit &= "1000100"

                Case 8

                    strExit &= "1001000"

                Case 9

                    strExit &= "1110100"

            End Select

        Next K

              strExit &= "101000"

        EAN2Bin = strExit

    End Function

 

 

Now write the function as written below. this function actually Prints the barcode on a picture box by extract values of barcode from above function.

It also Convert the code on its binary representation. Define the font to be printed. Defines the boundaries to the barcode, Defines the boundaries of the barcode and also Clears the area and Calculates the scale Draw the BarCode. And Finally Draw the code which is entered by user.

Public Sub PrintEANBarCode(ByVal strEANCode As String, ByVal objPicBox As PictureBox, _

                                Optional ByVal sngX1 As Single = (-1), _

                                Optional ByVal sngY1 As Single = (-1), _

                                Optional ByVal sngX2 As Single = (-1), _

                                Optional ByVal sngY2 As Single = (-1), _

                                Optional ByVal FontForText As Font = Nothing)

        Dim K As Single

        Dim sngPosX As Single

        Dim sngPosY As Single

        Dim sngScaleX As Single

        Dim strEANBin As String

        Dim strFormat As New StringFormat

        strEANBin = EAN2Bin(strEANCode)

        If (FontForText Is Nothing) Then

            FontForText = New Font("Courier New", 10)

        End If

        If sngX1 = (-1) Then sngX1 = 0

        If sngY1 = (-1) Then sngY1 = 0

        If sngX2 = (-1) Then sngX2 = objPicBox.Width

        If sngY2 = (-1) Then sngY2 = objPicBox.Height

                sngPosX = sngX1

        sngPosY = sngY2 - CSng(1.5 * FontForText.Height)

                objPicBox.CreateGraphics.FillRectangle(New System.Drawing.SolidBrush(objPicBox.BackColor.Blue), sngX1, sngY1, sngX2 - sngX1, sngY2 - sngY1)

        For K = 1 To Len(strEANBin)

            If Mid(strEANBin, K, 1) = "1" Then

                objPicBox.CreateGraphics.FillRectangle(New System.Drawing.SolidBrush(objPicBox.ForeColor.Red), sngPosX, sngY1, sngScaleX, sngPosY)

            End If

            sngPosX = sngX1 + (K * sngScaleX)

        Next K

               strFormat.Alignment = StringAlignment.Center

        strFormat.FormatFlags = StringFormatFlags.NoWrap

        objPicBox.CreateGraphics.DrawString(strEANCode, FontForText, New System.Drawing.SolidBrush(objPicBox.ForeColor.Orange), CSng((sngX2 - sngX1) / 2), CSng(sngY2 - FontForText.Height), strFormat)

    End Sub

 

This is a simple article .i hope u will like and enjoying that Article. If you feel any difficulty then reference the source code.because Source code is given with this for your proper understanding.

Article Comments
how to make this command..
can u tell me properly..
i need to use it
thanks...

Posted on 11/12/2009 9:47:38 PM by naseh

Hi Naseh,

Use these functions directly into your VB.net application, it will work fine.

Thanks

Posted on 11/13/2009 4:30:39 AM by zunnair

hi zunnair i have problem in that code give me Error('Public Property Left() As Integer' has no parameters and its return type cannot be indexed).
I don't no how to generate bar code from vb.net as i have college project any help ll b appreciate.
Thanks in advance..

Posted on 11/22/2009 8:47:52 PM by Khanjee

Hi Khanjee,

Please mail me project, in which you are using this code. Then i will be able to know where the problem is
zunair.rafiq1@gmail.com

Thanks

Posted on 11/24/2009 7:07:14 AM by zunnair

hi zunnair the code has an error same as said by Khanjee. Can you please help me out to correct this.
Thanks in advance

Posted on 11/30/2009 4:38:07 AM by Jayapragash

Hi jayapragash,

This code working fine in older version of visual studio, now i am looking into code for newer version when i solve this problem then i let you know.

THanks.

Posted on 11/30/2009 4:41:54 AM by zunnair

hi
iam using code 128 for this function i used this code. but it will print oly for numeric.
can u tell me for code 128??

Thanks

Posted on 12/14/2009 3:46:35 AM by Kasinathan

It doesn't seem to be making the bars but it show the numbers fine.

Posted on 12/30/2009 7:38:09 AM by Adam

hi,
have u this application in c#.net..please help me


Posted on 1/20/2010 1:08:26 AM by Raja

I have the problem of the left also, if someone I solve this case, please and if they have for alphanumeric codes
Thank you

Posted on 1/28/2010 8:41:45 AM by Elio

if you have problem with Left function, you can correct like this;

CInt(Microsoft.VisualBasic.Left(strEANCode, 1))

I have also have problem with this line...
objPicBox.CreateGraphics.FillRectangle(New System.Drawing.SolidBrush(objPicBox.BackColor.Blue), sngX1, sngY1, sngX2 - sngX1, sngY2 - sngY1)

vb doesn't like BackColor property. it offers "Color". and then this code doesn't work...
any idea!!??
help please..

Posted on 1/31/2010 2:15:46 PM by Judasis

Thank u for the code. But i have a project that needs to save this barcode into a picture. how can i save this using SaveFileDialog in a JPEG format or any format?

Posted on 2/10/2010 10:53:29 PM by bhebhe_08

Is that possible this code used in vb.net web application.
If yes Please reply.

Posted on 2/16/2010 6:41:00 AM by Parveen

can i get the source code in c#.net asp.net application

Posted on 2/24/2010 11:33:27 PM by hare

Hello..
This is subash.. I need a vb.net sample project which reads & generates barcode using code39 format.. also intimate twhere those packages would be stored by default... Kindly reply me as quick as possible.. Infact, mail me the source code if possible..because i need to complete the project as quick as possible

Posted on 3/20/2010 12:14:57 AM by Subash

Thank you for this code it will help

Posted on 4/13/2010 11:14:37 AM by William Ali

HI all..
plz i need to integrate barcode generator in my vb.net project could u provide any reference or code for doing this

Posted on 5/31/2010 9:32:01 AM by pasha

plz i need to integrate barcode generator in my vb.net project could u provide any reference or code for doing this

haneefa

Posted on 6/27/2010 8:38:27 AM by haneefa

Zunnair,

I have problem with the line
objPicBox.CreateGraphics.FillRectangle(New System.Drawing.SolidBrush(objPicBox.BackColor.Blue), sngX1, sngY1, sngX2 - sngX1, sngY2 - sngY1)

BackColo

The warning is :
Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated.

And thus I believe this line is not being evaluated.
Any thoughts?


Bash

Posted on 9/1/2010 1:09:44 AM by Bash

Assalamu Alaikum.
Please can you send me code for barcode because I am final year student of Kano University of Science and Technology, Nigeria. I am doing my project on Barcode or even ebook on barcode.
Here is my email sarkibasheer@yahoo.com

Posted on 11/9/2010 4:03:06 PM by Bashir Sarki

Zunnair
please can you send me a code in generating a barcode into a picture with 8 numbers because i dont really have an idea..
i am using vb 2008...please help me.thank you.. to cngerra@yahoo.com

Posted on 12/1/2010 11:04:44 AM by chris

sir we have a project that need to program the barcode scanner using visual basic

can you send to me a sample program.

please....

can you input some of the codes

Posted on 12/1/2010 9:09:10 PM by wilmer

Hi

For getting bar code input put your control of GUI in a textbox and then just press bar code button to scan. You Operating system automatically put value in that text box.

Thanks.

Posted on 12/2/2010 11:26:15 AM by Zunair

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Working with Check List Box in VB.Net

Disclaimer - Privacy
© 2002-2014 DevASP.net