Click here to Skip to main content
15,121,891 members
Articles / Programming Languages / Visual Basic
Tip/Trick
Posted 20 Jan 2018

Tagged as

Stats

29.7K views
4.7K downloads
10 bookmarked

VB.NET Code 128 (B) Barcode Generator/Creator

Rate me:
Please Sign up or sign in to vote.
4.20/5 (10 votes)
20 Jan 2018CPOL
Creating Code 128B barcodes with VB.NET

Image 1

Introduction

I created this with Visual Studio 2017. I used the information off Wiki to make a VB Barcode generator. It seems to work well. I don't know how to print the contents of the picturebox. So this means the print button doesn't work. Short of that, i have tested it and it does work well. This code can easily be modified for 128A. I'm an amateur so needless to say, my code is sloppy. Feel free to give me any pointers.

Background

I used the "Bar code widths" section of "Code 128" of wiki to generate this. I cheated a little and copied the table into Excel, then created a formula that would write my code for me. It didn't take long to do. I just had to use the "Char(34)" a lot in my formulas.

Using the Code

There are three main functions that really do all the work:

  1. GenerateBarcode(Input as String)
  2. GenerateCheckSum(Dig as Integer)
  3. GenerateBINARY(Input As String)

First, turn your string into binary.

C++
//GenerateBINARY(txtInput.Text) 'textbox.text

Note: "GenerateBINARY(Input As String)" uses "GenerateCheckSum(Dig as Integer)" in the function.

Second, turn the BINARY of the first step into Barcode 128B.

// GenerateBarcode(BINARY)  ' "BINARY" is the result of the first step

Source Code

VB.NET
Imports System
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Printing
Imports System.Windows.Forms

Public Class Form1

    Dim BINARY As String
    Dim CheckSumVal As Integer
    Dim BINVAL As String
    Dim BINARYLength As Int32

    Friend WithEvents printDoc As New PrintDocument()
    Dim bmpBarcode As Bitmap

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        picBarcode.BackColor = Color.White

        Dim FontSize As Integer = 0
        Dim LengthOfBarcode As Integer = 10

        FontSize = 1
        Do Until FontSize = 72
            cboSize.Items.Add(FontSize)
            FontSize = FontSize + 1
        Loop

        Do Until LengthOfBarcode = 110
            cboLength.Items.Add(LengthOfBarcode)
            LengthOfBarcode = LengthOfBarcode + 10
        Loop

        cboSize.SelectedIndex = 0
        cboLength.SelectedIndex = 9

    End Sub

    Private Sub btnGenerate_Click(sender As Object, e As EventArgs) Handles btnGenerate.Click

        GenerateBINARY(txtInput.Text)
        GenerateBarcode(BINARY)

    End Sub

    Private Sub GenerateBINARY(Input As String)

        BINARY = 0

        Dim CharVal As Integer = 0
        Dim CharCount As Integer = 0
        Dim GrandCharCount As Integer = 0
        Dim CheckSum As Integer = 0

        For Each Item As Char In Input

            Select Case Item
                Case = " "
                    BINARY = BINARY & "11011001100"
                    CharVal = 0
                Case = "!"
                    BINARY = BINARY & "11001101100"
                    CharVal = 1
                Case = """"
                    BINARY = BINARY & "11001100110"
                    CharVal = 2
                Case = "#"
                    BINARY = BINARY & "10010011000"
                    CharVal = 3
                Case = "$"
                    BINARY = BINARY & "10010001100"
                    CharVal = 4
                Case "%"
                    BINARY = BINARY & "10001001100"
                    CharVal = 5
                Case "&"
                    BINARY = BINARY & "10011001000"
                    CharVal = 6
                Case "'"
                    BINARY = BINARY & "10011000100"
                    CharVal = 7
                Case "("
                    BINARY = BINARY & "10001100100"
                    CharVal = 8
                Case ")"
                    BINARY = BINARY & "11001001000"
                    CharVal = 9
                Case "*"
                    BINARY = BINARY & "11001000100"
                    CharVal = 10
                Case "+"
                    BINARY = BINARY & "11000100100"
                    CharVal = 11
                Case ","
                    BINARY = BINARY & "10110011100"
                    CharVal = 12
                Case "-"
                    BINARY = BINARY & "10011011100"
                    CharVal = 13
                Case "."
                    BINARY = BINARY & "10011001110"
                    CharVal = 14
                Case "/"
                    BINARY = BINARY & "10111001100"
                    CharVal = 15
                Case "0"
                    BINARY = BINARY & "10011101100"
                    CharVal = 16
                Case "1"
                    BINARY = BINARY & "10011100110"
                    CharVal = 17
                Case "2"
                    BINARY = BINARY & "11001110010"
                    CharVal = 18
                Case "3"
                    BINARY = BINARY & "11001011100"
                    CharVal = 19
                Case "4"
                    BINARY = BINARY & "11001001110"
                    CharVal = 20
                Case "5"
                    BINARY = BINARY & "11011100100"
                    CharVal = 21
                Case "6"
                    BINARY = BINARY & "11001110100"
                    CharVal = 22
                Case "7"
                    BINARY = BINARY & "11101101110"
                    CharVal = 23
                Case "8"
                    BINARY = BINARY & "11101001100"
                    CharVal = 24
                Case "9"
                    BINARY = BINARY & "11100101100"
                    CharVal = 25
                Case ":"
                    BINARY = BINARY & "11100100110"
                    CharVal = 26
                Case " ;"
                    BINARY = BINARY & "11101100100"
                    CharVal = 27
                Case "<"
                    BINARY = BINARY & "11100110100"
                    CharVal = 28
                Case "="
                    BINARY = BINARY & "11100110010"
                    CharVal = 29
                Case ">"
                    BINARY = BINARY & "11011011000"
                    CharVal = 30
                Case " ?"
                    BINARY = BINARY & "11011000110"
                    CharVal = 31
                Case "@"
                    BINARY = BINARY & "11000110110"
                    CharVal = 32
                Case "A"
                    BINARY = BINARY & "10100011000"
                    CharVal = 33
                Case "B"
                    BINARY = BINARY & "10001011000"
                    CharVal = 34
                Case "C"
                    BINARY = BINARY & "10001000110"
                    CharVal = 35
                Case "D"
                    BINARY = BINARY & "10110001000"
                    CharVal = 36
                Case "E"
                    BINARY = BINARY & "10001101000"
                    CharVal = 37
                Case "F"
                    BINARY = BINARY & "10001100010"
                    CharVal = 38
                Case "G"
                    BINARY = BINARY & "11010001000"
                    CharVal = 39
                Case "H"
                    BINARY = BINARY & "11000101000"
                    CharVal = 40
                Case "I"
                    BINARY = BINARY & "11000100010"
                    CharVal = 41
                Case "J"
                    BINARY = BINARY & "10110111000"
                    CharVal = 42
                Case "K"
                    BINARY = BINARY & "10110001110"
                    CharVal = 43
                Case "L"
                    BINARY = BINARY & "10001101110"
                    CharVal = 44
                Case "M"
                    BINARY = BINARY & "10111011000"
                    CharVal = 45
                Case "N"
                    BINARY = BINARY & "10111000110"
                    CharVal = 46
                Case "O"
                    BINARY = BINARY & "10001110110"
                    CharVal = 47
                Case "P"
                    BINARY = BINARY & "11101110110"
                    CharVal = 48
                Case "Q"
                    BINARY = BINARY & "11010001110"
                    CharVal = 49
                Case "R"
                    BINARY = BINARY & "11000101110"
                    CharVal = 50
                Case "S"
                    BINARY = BINARY & "11011101000"
                    CharVal = 51
                Case "T"
                    BINARY = BINARY & "11011100010"
                    CharVal = 52
                Case "U"
                    BINARY = BINARY & "11011101110"
                    CharVal = 53
                Case "V"
                    BINARY = BINARY & "11101011000"
                    CharVal = 54
                Case "W"
                    BINARY = BINARY & "11101000110"
                    CharVal = 55
                Case "X"
                    BINARY = BINARY & "11100010110"
                    CharVal = 56
                Case "Y"
                    BINARY = BINARY & "11101101000"
                    CharVal = 57
                Case "Z"
                    BINARY = BINARY & "11101100010"
                    CharVal = 58
                Case "["
                    BINARY = BINARY & "11100011010"
                    CharVal = 59
                Case "\"
                    BINARY = BINARY & "11101111010"
                    CharVal = 60
                Case "]"
                    BINARY = BINARY & "11001000010"
                    CharVal = 61
                Case "^"
                    BINARY = BINARY & "11110001010"
                    CharVal = 62
                Case "_"
                    BINARY = BINARY & "10100110000"
                    CharVal = 63
                Case "`"
                    BINARY = BINARY & "10100001100"
                    CharVal = 64
                Case "a"
                    BINARY = BINARY & "10010110000"
                    CharVal = 65
                Case "b"
                    BINARY = BINARY & "10010000110"
                    CharVal = 66
                Case "c"
                    BINARY = BINARY & "10000101100"
                    CharVal = 67
                Case "d"
                    BINARY = BINARY & "10000100110"
                    CharVal = 68
                Case "e"
                    BINARY = BINARY & "10110010000"
                    CharVal = 69
                Case "f"
                    BINARY = BINARY & "10110000100"
                    CharVal = 70
                Case "g"
                    BINARY = BINARY & "10011010000"
                    CharVal = 71
                Case "h"
                    BINARY = BINARY & "10011000010"
                    CharVal = 72
                Case "i"
                    BINARY = BINARY & "10000110100"
                    CharVal = 73
                Case "j"
                    BINARY = BINARY & "10000110010"
                    CharVal = 74
                Case "k"
                    BINARY = BINARY & "11000010010"
                    CharVal = 75
                Case "l"
                    BINARY = BINARY & "11001010000"
                    CharVal = 76
                Case "m"
                    BINARY = BINARY & "11110111010"
                    CharVal = 77
                Case "n"
                    BINARY = BINARY & "11000010100"
                    CharVal = 78
                Case "o"
                    BINARY = BINARY & "10001111010"
                    CharVal = 79
                Case "p"
                    BINARY = BINARY & "10100111100"
                    CharVal = 80
                Case "q"
                    BINARY = BINARY & "10010111100"
                    CharVal = 81
                Case "r"
                    BINARY = BINARY & "10010011110"
                    CharVal = 82
                Case "s"
                    BINARY = BINARY & "10111100100"
                    CharVal = 83
                Case "t"
                    BINARY = BINARY & "10011110100"
                    CharVal = 84
                Case "u"
                    BINARY = BINARY & "10011110010"
                    CharVal = 85
                Case "v"
                    BINARY = BINARY & "11110100100"
                    CharVal = 86
                Case "w"
                    BINARY = BINARY & "11110010100"
                    CharVal = 87
                Case "x"
                    BINARY = BINARY & "11110010010"
                    CharVal = 88
                Case "y"
                    BINARY = BINARY & "11011011110"
                    CharVal = 89
                Case "z"
                    BINARY = BINARY & "11011110110"
                    CharVal = 90
                Case "{"
                    BINARY = BINARY & "11110110110"
                    CharVal = 91
                Case "|"
                    BINARY = BINARY & "10101111000"
                    CharVal = 92
                Case "}"
                    BINARY = BINARY & "10100011110"
                    CharVal = 93
                Case "~"
                    BINARY = BINARY & "10001011110"
                    CharVal = 94
            End Select
            CharCount = CharCount + 1
            GrandCharCount = CharVal * CharCount
            CheckSum = CheckSum + GrandCharCount

        Next

        'Include start code B 
        CheckSum = CheckSum + 104
        'Get the remainder
        CheckSumVal = CheckSum Mod 103

        'Set it to zero before we generate it
        BINVAL = 0
        GenerateCheckSum(CheckSumVal)

        'Start
        BINARY = "00011010010000" & BINARY
        'End
        BINARY = BINARY & BINVAL & "1100011101011000"
        '    

        BINARYLength = 0
        For Each dig As String In BINARY
            CharCount = CharCount + 1
        Next

    End Sub

    Private Sub GenerateCheckSum(Dig As Integer)

        Select Case Dig
            Case "0"
                BINVAL = "11011001100"
            Case "1"
                BINVAL = "11001101100"
            Case "2"
                BINVAL = "11001100110"
            Case "3"
                BINVAL = "10010011000"
            Case "4"
                BINVAL = "10010001100"
            Case "5"
                BINVAL = "10001001100"
            Case "6"
                BINVAL = "10011001000"
            Case "7"
                BINVAL = "10011000100"
            Case "8"
                BINVAL = "10001100100"
            Case "9"
                BINVAL = "11001001000"
            Case "10"
                BINVAL = "11001000100"
            Case "11"
                BINVAL = "11000100100"
            Case "12"
                BINVAL = "10110011100"
            Case "13"
                BINVAL = "10011011100"
            Case "14"
                BINVAL = "10011001110"
            Case "15"
                BINVAL = "10111001100"
            Case "16"
                BINVAL = "10011101100"
            Case "17"
                BINVAL = "10011100110"
            Case "18"
                BINVAL = "11001110010"
            Case "19"
                BINVAL = "11001011100"
            Case "20"
                BINVAL = "11001001110"
            Case "21"
                BINVAL = "11011100100"
            Case "22"
                BINVAL = "11001110100"
            Case "23"
                BINVAL = "11101101110"
            Case "24"
                BINVAL = "11101001100"
            Case "25"
                BINVAL = "11100101100"
            Case "26"
                BINVAL = "11100100110"
            Case "27"
                BINVAL = "11101100100"
            Case "28"
                BINVAL = "11100110100"
            Case "29"
                BINVAL = "11100110010"
            Case "30"
                BINVAL = "11011011000"
            Case "31"
                BINVAL = "11011000110"
            Case "32"
                BINVAL = "11000110110"
            Case "33"
                BINVAL = "10100011000"
            Case "34"
                BINVAL = "10001011000"
            Case "35"
                BINVAL = "10001000110"
            Case "36"
                BINVAL = "10110001000"
            Case "37"
                BINVAL = "10001101000"
            Case "38"
                BINVAL = "10001100010"
            Case "39"
                BINVAL = "11010001000"
            Case "40"
                BINVAL = "11000101000"
            Case "41"
                BINVAL = "11000100010"
            Case "42"
                BINVAL = "10110111000"
            Case "43"
                BINVAL = "10110001110"
            Case "44"
                BINVAL = "10001101110"
            Case "45"
                BINVAL = "10111011000"
            Case "46"
                BINVAL = "10111000110"
            Case "47"
                BINVAL = "10001110110"
            Case "48"
                BINVAL = "11101110110"
            Case "49"
                BINVAL = "11010001110"
            Case "50"
                BINVAL = "11000101110"
            Case "51"
                BINVAL = "11011101000"
            Case "52"
                BINVAL = "11011100010"
            Case "53"
                BINVAL = "11011101110"
            Case "54"
                BINVAL = "11101011000"
            Case "55"
                BINVAL = "11101000110"
            Case "56"
                BINVAL = "11100010110"
            Case "57"
                BINVAL = "11101101000"
            Case "58"
                BINVAL = "11101100010"
            Case "59"
                BINVAL = "11100011010"
            Case "60"
                BINVAL = "11101111010"
            Case "61"
                BINVAL = "11001000010"
            Case "62"
                BINVAL = "11110001010"
            Case "63"
                BINVAL = "10100110000"
            Case "64"
                BINVAL = "10100001100"
            Case "65"
                BINVAL = "10010110000"
            Case "66"
                BINVAL = "10010000110"
            Case "67"
                BINVAL = "10000101100"
            Case "68"
                BINVAL = "10000100110"
            Case "69"
                BINVAL = "10110010000"
            Case "70"
                BINVAL = "10110000100"
            Case "71"
                BINVAL = "10011010000"
            Case "72"
                BINVAL = "10011000010"
            Case "73"
                BINVAL = "10000110100"
            Case "74"
                BINVAL = "10000110010"
            Case "75"
                BINVAL = "11000010010"
            Case "76"
                BINVAL = "11001010000"
            Case "77"
                BINVAL = "11110111010"
            Case "78"
                BINVAL = "11000010100"
            Case "79"
                BINVAL = "10001111010"
            Case "80"
                BINVAL = "10100111100"
            Case "81"
                BINVAL = "10010111100"
            Case "82"
                BINVAL = "10010011110"
            Case "83"
                BINVAL = "10111100100"
            Case "84"
                BINVAL = "10011110100"
            Case "85"
                BINVAL = "10011110010"
            Case "86"
                BINVAL = "11110100100"
            Case "87"
                BINVAL = "11110010100"
            Case "88"
                BINVAL = "11110010010"
            Case "89"
                BINVAL = "11011011110"
            Case "90"
                BINVAL = "11011110110"
            Case "91"
                BINVAL = "11110110110"
            Case "92"
                BINVAL = "10101111000"
            Case "93"
                BINVAL = "10100011110"
            Case "94"
                BINVAL = "10001011110"

        End Select

    End Sub

    Private Sub GenerateBarcode(Input As String)

        Dim num As Integer = 0
        For Each one As String In Input
            num = num + cboSize.Text
        Next

        Dim rec As New Rectangle(1, 1, num, cboLength.Text)
        Dim img As New Bitmap(num, Convert.ToInt32(cboLength.Text))

        Dim count As Integer = 0
        Dim length As Integer = 0

        Dim aBlackPen As New Pen(Color.Black)
        Dim aWhitePen As New Pen(Color.White)

        aBlackPen.Width = cboSize.Text
        aWhitePen.Width = cboSize.Text

        length = length + cboLength.Text

        For Each item As String In Input
            count = count + cboSize.Text

            If item = 1 Then
                picBarcode.CreateGraphics.DrawLine(aBlackPen, count, 1, count, length)

            Else
                picBarcode.CreateGraphics.DrawLine(aWhitePen, count, 1, count, length)

            End If

        Next

        picBarcode.DrawToBitmap(img, rec)

        bmpBarcode = img

    End Sub

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        picBarcode.Image = Nothing
    End Sub

    Private Sub PrintImage(ByVal sender As Object, _
        ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles printDoc.PrintPage

        e.Graphics.DrawImage(bmpBarcode, e.MarginBounds.Left, e.MarginBounds.Top)

    End Sub

    Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click

        printDoc.Print()

    End Sub
End Class

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

fah_q_2007
United States United States
No Biography provided

Comments and Discussions

 
BugFound an issue with Checksum code Pin
tony servies29-Jan-19 9:17
Membertony servies29-Jan-19 9:17 
SuggestionPrinting the barcode Pin
gilman66623-Jan-18 0:45
Membergilman66623-Jan-18 0:45 
PraiseThanks for the post. Now always using code128 fonts Pin
Rene Vliex22-Jan-18 22:39
MemberRene Vliex22-Jan-18 22:39 
GeneralMy vote of 4 Pin
keary_keller22-Jan-18 8:27
Memberkeary_keller22-Jan-18 8:27 
Awesome! I like it when people go to the work to build it from scratch to understand what it takes to make it work. Sloppy code or not - great way to learn! Keep up the good work!
SuggestionImplementing barcode Clipboard copy Pin
Ari Ruottu22-Jan-18 2:12
MemberAri Ruottu22-Jan-18 2:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.