只是一个简单的封装。调用该类的方法后,会返回形如 bsbssbsssbbbssbbssbbss 这样的字符串。然后您可以根据这串字符,指定位置和打印介质来打印到picturebox或者打印机。
打印效果:
封装代码:
Public Class clsBarcode
Public Shared Function get_code_39(ByVal barcode As String)
Dim strBarTable As New Dictionary(Of String, String), final As String = ""
strBarTable.Add("0", "001100100") ' 0
strBarTable.Add("1", "100010100") ' 1
strBarTable.Add("2", "010010100") ' 2
strBarTable.Add("3", "110000100") ' 3
strBarTable.Add("4", "001010100") ' 4
strBarTable.Add("5", "101000100") ' 5
strBarTable.Add("6", "011000100") ' 6
strBarTable.Add("7", "000110100") ' 7
strBarTable.Add("8", "100100100") ' 8
strBarTable.Add("9", "010100100") ' 9
strBarTable.Add("A", "100010010") ' A
strBarTable.Add("B", "010010010") ' B
strBarTable.Add("C", "110000010") ' C
strBarTable.Add("D", "001010010") ' D
strBarTable.Add("E", "101000010") ' E
strBarTable.Add("F", "011000010") ' F
strBarTable.Add("G", "000110010") ' G
strBarTable.Add("H", "100100010") ' H
strBarTable.Add("I", "010100010") ' I
strBarTable.Add("J", "001100010") ' J
strBarTable.Add("K", "100010001") ' K
strBarTable.Add("L", "010010001") ' L
strBarTable.Add("M", "110000001") ' M
strBarTable.Add("N", "001010001") ' N
strBarTable.Add("O", "101000001") ' O
strBarTable.Add("P", "011000001") ' P
strBarTable.Add("Q", "000110001") ' Q
strBarTable.Add("R", "100100001") ' R
strBarTable.Add("S", "010100001") ' S
strBarTable.Add("T", "001100001") ' T
strBarTable.Add("U", "100011000") ' U
strBarTable.Add("V", "010011000") ' V
strBarTable.Add("W", "110001000") ' W
strBarTable.Add("X", "001011000") ' X
strBarTable.Add("Y", "101001000") ' Y
strBarTable.Add("Z", "011001000") ' Z
strBarTable.Add("-", "000111000") ' -
strBarTable.Add("%", "100101000") ' %
strBarTable.Add("$", "010101000") ' $
strBarTable.Add("*", "001101000") ' *
barcode = "*" + barcode + "*"
For i = 1 To barcode.Length
For j = 1 To 5
If strBarTable.Item(barcode.Substring(i - 1, 1)).Substring(j - 1, 1) = "0" Then
final += "b"
Else
final += "bbb"
End If
If j = 5 Then
final += StrDup(2, "s")
Exit For
End If
If strBarTable.Item(barcode.Substring(i - 1, 1)).Substring(j + 4, 1) = "0" Then
final += StrDup(1, "s") 'StrDup(2, "s")
Else
final += StrDup(2, "s") 'StrDup(3, "s")
End If
Next j
Next i
Return final
End Function
Public Shared Function get_code_128Auto(ByVal barcode As String)
Dim final As String = "", laststr = "", exam As String, strr As String
Dim code128 As Dictionary(Of String, String()) = code128_num()
Dim code128_byid As Dictionary(Of String, String()) = code128_table()
If barcode.Length Mod 2 = 0 Then
final = get_code_128C(barcode, True)
Else
'前12位codec编码
final += get_code_128C(barcode.Substring(0, barcode.Length - 1), False)
laststr = barcode.Substring(barcode.Length - 1, 1)
'codeA
final += code128_byid.Item("101")(4)
'最后一位编码
final += code128.Item(laststr)(0)
'校验码
exam = code128.Item("StartC")(1).ToString
Dim k As Int16 = 1
For i = 0 To barcode.Length - 2 Step 2
strr = barcode.Substring(i, 2)
If strr.Substring(0, 1) = "0" Then
strr = strr.Substring(1, 1)
End If
exam += CInt(strr) * k
k += 1
Next i
exam += 101 * k
k += 1
exam += code128.Item(barcode.Substring(barcode.Length - 1, 1))(1) * k
exam = exam Mod 103
Dim excode As String = code128_byid.Item(exam)(4)
final &= excode
'stop
final &= code128.Item("Stop")(0)
End If
Return final
End Function
Public Shared Function get_code_128C(ByVal barcode As String, withExam As Boolean)
Dim code128 As Dictionary(Of String, String()) = code128_num()
Dim code128_byid As Dictionary(Of String, String()) = code128_table()
If barcode.Length Mod 2 <> 0 Then
MsgBox("128c条码长度必须为偶数")
Return ""
End If
Dim chr As String, final As String = ""
Dim start As String = code128.Item("StartC")(0)
final += start
'数据
For i = 0 To barcode.Length - 1 Step 2
chr = barcode.Substring(i, 2)
If chr.Substring(0, 1) = "0" Then
chr = chr.Substring(1, 1)
End If
Debug.Print(code128_byid.Item(chr)(4))
final += code128_byid.Item(chr)(4)
Next i
If withExam Then
final += code128_end(barcode, "C", code128, code128_byid)
End If
Return final
End Function
Private Shared Function code128_end(barcode As String, type As Char, code128 As Dictionary(Of String, String()), code128_byid As Dictionary(Of String, String())) As String
Dim final As String = "", strr As String
'校验
Dim exam As Int32
exam = code128.Item("Start" & type)(1).ToString
Dim k As Int16 = 1
For i = 0 To barcode.Length - 1 Step 2
strr = barcode.Substring(i, 2)
If strr.Substring(0, 1) = "0" Then
strr = strr.Substring(1, 1)
End If
exam += CInt(strr) * k
k += 1
Next i
exam = exam Mod 103
Dim excode As String = code128_byid.Item(exam)(4)
final &= excode
'stop
final += code128.Item("Stop")(0)
Return final
End Function
Public Shared Function get_code_128A(ByVal barcode As String)
Dim strr As String = barcode
Dim code128 As Dictionary(Of String, String()) = code128_num()
Dim code128_byid As Dictionary(Of String, String()) = code128_table()
Dim chr As Int16, barstr As String, final As String = ""
Dim start As String = code128.Item("StartA")(0)
final += start
'数据
For i = 0 To strr.Length - 1
chr = strr.Substring(i, 1)
barstr = code128.Item(chr)(0)
final += barstr
Next i
'校验
Dim exam As Int16
exam = code128.Item("StartA")(1)
For i = 0 To strr.Length - 1
exam += code128.Item(strr.Substring(i, 1))(1) * (i + 1)
Next i
exam = exam Mod code128.Item("StartA")(1)
Dim excode As String = code128_byid.Item(exam)(4)
final &= excode
'stop
final += code128.Item("Stop")(0)
Return final
End Function
Private Shared Function code128_num()
Dim code128 As New Dictionary(Of String, String())
code128.Add("0", {"bssbbbsbbss", 16})
code128.Add("1", {"bssbbbssbbs", 17})
code128.Add("2", {"bbssbbbssbs", 18})
code128.Add("3", {"bbssbsbbbss", 19})
code128.Add("4", {"bbssbssbbbs", 20})
code128.Add("5", {"bbsbbbssbss", 21})
code128.Add("6", {"bbssbbbsbss", 22})
code128.Add("7", {"bbbsbbsbbbs", 23})
code128.Add("8", {"bbbsbssbbss", 24})
code128.Add("9", {"bbbssbsbbss", 25})
code128.Add("StartA", {"bbsbssssbss", 103})
code128.Add("StartB", {"bbsbssbssss", 104})
code128.Add("StartC", {"bbsbssbbbss", 105})
code128.Add("Stop", {"bbsssbbbsbsbb", 106})
Return code128
End Function
Private Shared Function code128_table()
Dim code128_byid As New Dictionary(Of String, String())
code128_byid.Add("0", {"SP", "SP", "0", "212222", "bbsbbssbbss"})
code128_byid.Add("1", {"!", "!", "1", "222122", "bbssbbsbbss"})
code128_byid.Add("2", {"““", "““", "2", "222221", "bbssbbssbbs"})
code128_byid.Add("3", {"#", "#", "3", "121223", "bssbssbbsss"})
code128_byid.Add("4", {"$", "$", "4", "121322", "bssbsssbbss"})
code128_byid.Add("5", {"%", "%", "5", "131222", "bsssbssbbss"})
code128_byid.Add("6", {"&", "&", "6", "122213", "bssbbssbsss"})
code128_byid.Add("7", {"‘", "‘", "7", "122312", "bssbbsssbss"})
code128_byid.Add("8", {"(", "(", "8", "132212", "bsssbbssbss"})
code128_byid.Add("9", {")", ")", "9", "221213", "bbssbssbsss"})
code128_byid.Add("10", {"*", "*", "10", "221312", "bbssbsssbss"})
code128_byid.Add("11", {"+", "+", "11", "231212", "bbsssbssbss"})
code128_byid.Add("12", {",", ",", "12", "112232", "bsbbssbbbss"})
code128_byid.Add("13", {"-", "-", "13", "122132", "bssbbsbbbss"})
code128_byid.Add("14", {".", ".", "14", "122231", "bssbbssbbbs"})
code128_byid.Add("15", {"/", "/", "15", "113222", "bsbbbssbbss"})
code128_byid.Add("16", {"0", "0", "16", "123122", "bssbbbsbbss"})
code128_byid.Add("17", {"1", "1", "17", "123221", "bssbbbssbbs"})
code128_byid.Add("18", {"2", "2", "18", "223211", "bbssbbbssbs"})
code128_byid.Add("19", {"3", "3", "19", "221132", "bbssbsbbbss"})
code128_byid.Add("20", {"4", "4", "20", "221231", "bbssbssbbbs"})
code128_byid.Add("21", {"5", "5", "21", "213212", "bbsbbbssbss"})
code128_byid.Add("22", {"6", "6", "22", "223112", "bbssbbbsbss"})
code128_byid.Add("23", {"7", "7", "23", "312131", "bbbsbbsbbbs"})
code128_byid.Add("24", {"8", "8", "24", "311222", "bbbsbssbbss"})
code128_byid.Add("25", {"9", "9", "25", "321122", "bbbssbsbbss"})
code128_byid.Add("26", {":", ":", "26", "321221", "bbbssbssbbs"})
code128_byid.Add("27", {";", ";", "27", "312212", "bbbsbbssbss"})
code128_byid.Add("28", {"<", "<", "28", "322112", "bbbssbbsbss"})
code128_byid.Add("29", {"=", "=", "29", "322211", "bbbssbbssbs"})
code128_byid.Add("30", {">", ">", "30", "212123", "bbsbbsbbsss"})
code128_byid.Add("31", {"?", "?", "31", "212321", "bbsbbsssbbs"})
code128_byid.Add("32", {"@", "@", "32", "232121", "bbsssbbsbbs"})
code128_byid.Add("33", {"A", "A", "33", "111323", "bsbsssbbsss"})
code128_byid.Add("34", {"B", "B", "34", "131123", "bsssbsbbsss"})
code128_byid.Add("35", {"C", "C", "35", "131321", "bsssbsssbbs"})
code128_byid.Add("36", {"D", "D", "36", "112313", "bsbbsssbsss"})
code128_byid.Add("37", {"E", "E", "37", "132113", "bsssbbsbsss"})
code128_byid.Add("38", {"F", "F", "38", "132311", "bsssbbsssbs"})
code128_byid.Add("39", {"G", "G", "39", "211313", "bbsbsssbsss"})
code128_byid.Add("40", {"H", "H", "40", "231113", "bbsssbsbsss"})
code128_byid.Add("41", {"I", "I", "41", "231311", "bbsssbsssbs"})
code128_byid.Add("42", {"J", "J", "42", "112133", "bsbbsbbbsss"})
code128_byid.Add("43", {"K", "K", "43", "112331", "bsbbsssbbbs"})
code128_byid.Add("44", {"L", "L", "44", "132131", "bsssbbsbbbs"})
code128_byid.Add("45", {"M", "M", "45", "113123", "bsbbbsbbsss"})
code128_byid.Add("46", {"N", "N", "46", "113321", "bsbbbsssbbs"})
code128_byid.Add("47", {"O", "O", "47", "133121", "bsssbbbsbbs"})
code128_byid.Add("48", {"P", "P", "48", "313121", "bbbsbbbsbbs"})
code128_byid.Add("49", {"Q", "Q", "49", "211331", "bbsbsssbbbs"})
code128_byid.Add("50", {"R", "R", "50", "231131", "bbsssbsbbbs"})
code128_byid.Add("51", {"S", "S", "51", "213113", "bbsbbbsbsss"})
code128_byid.Add("52", {"T", "T", "52", "213311", "bbsbbbsssbs"})
code128_byid.Add("53", {"U", "U", "53", "213131", "bbsbbbsbbbs"})
code128_byid.Add("54", {"V", "V", "54", "311123", "bbbsbsbbsss"})
code128_byid.Add("55", {"W", "W", "55", "311321", "bbbsbsssbbs"})
code128_byid.Add("56", {"X", "X", "56", "331121", "bbbsssbsbbs"})
code128_byid.Add("57", {"Y", "Y", "57", "312113", "bbbsbbsbsss"})
code128_byid.Add("58", {"Z", "Z", "58", "312311", "bbbsbbsssbs"})
code128_byid.Add("59", {"[", "[", "59", "332111", "bbbsssbbsbs"})
code128_byid.Add("60", {"\", "\", "60", "314111", "bbbsbbbbsbs"})
code128_byid.Add("61", {"]", "]", "61", "221411", "bbssbssssbs"})
code128_byid.Add("62", {"^", "^", "62", "431111", "bbbbsssbsbs"})
code128_byid.Add("63", {"_", "_", "63", "111224", "bsbssbbssss"})
code128_byid.Add("64", {"NUL", "`", "64", "111422", "bsbssssbbss"})
code128_byid.Add("65", {"SOH", "a", "65", "121124", "bssbsbbssss"})
code128_byid.Add("66", {"STX", "b", "66", "121421", "bssbssssbbs"})
code128_byid.Add("67", {"ETX", "c", "67", "141122", "bssssbsbbss"})
code128_byid.Add("68", {"EOT", "d", "68", "141221", "bssssbssbbs"})
code128_byid.Add("69", {"ENQ", "e", "69", "112214", "bsbbssbssss"})
code128_byid.Add("70", {"ACK", "f", "70", "112412", "bsbbssssbss"})
code128_byid.Add("71", {"BEL", "g", "71", "122114", "bssbbsbssss"})
code128_byid.Add("72", {"BS", "h", "72", "122411", "bssbbssssbs"})
code128_byid.Add("73", {"HT", "i", "73", "142112", "bssssbbsbss"})
code128_byid.Add("74", {"LF", "j", "74", "142211", "bssssbbssbs"})
code128_byid.Add("75", {"VT", "k", "75", "241211", "bbssssbssbs"})
code128_byid.Add("76", {"FF", "l", "76", "221114", "bbssbsbssss"})
code128_byid.Add("77", {"CR", "m", "77", "413111", "bbbbsbbbsbs"})
code128_byid.Add("78", {"SO", "n", "78", "241112", "bbssssbsbss"})
code128_byid.Add("79", {"SI", "o", "79", "134111", "bsssbbbbsbs"})
code128_byid.Add("80", {"DLE", "p", "80", "111242", "bsbssbbbbss"})
code128_byid.Add("81", {"DC1", "q", "81", "121142", "bssbsbbbbss"})
code128_byid.Add("82", {"DC2", "r", "82", "121241", "bssbssbbbbs"})
code128_byid.Add("83", {"DC3", "s", "83", "114212", "bsbbbbssbss"})
code128_byid.Add("84", {"DC4", "t", "84", "124112", "bssbbbbsbss"})
code128_byid.Add("85", {"NAK", "u", "85", "124211", "bssbbbbssbs"})
code128_byid.Add("86", {"SYN", "v", "86", "411212", "bbbbsbssbss"})
code128_byid.Add("87", {"ETB", "w", "87", "421112", "bbbbssbsbss"})
code128_byid.Add("88", {"CAN", "x", "88", "421211", "bbbbssbssbs"})
code128_byid.Add("89", {"EM", "y", "89", "212141", "bbsbbsbbbbs"})
code128_byid.Add("90", {"SUB", "z", "90", "214121", "bbsbbbbsbbs"})
code128_byid.Add("91", {"ESC", "{", "91", "412121", "bbbbsbbsbbs"})
code128_byid.Add("92", {"FS", "|", "92", "111143", "bsbsbbbbsss"})
code128_byid.Add("93", {"GS", "}", "93", "111341", "bsbsssbbbbs"})
code128_byid.Add("94", {"RS", "~", "94", "131141", "bsssbsbbbbs"})
code128_byid.Add("95", {"US", "DEL", "95", "114113", "bsbbbbsbsss"})
code128_byid.Add("96", {"FNC3", "FNC3", "96", "114311", "bsbbbbsssbs"})
code128_byid.Add("97", {"FNC2", "FNC2", "97", "411113", "bbbbsbsbsss"})
code128_byid.Add("98", {"SHIFT", "SHIFT", "98", "411311", "bbbbsbsssbs"})
code128_byid.Add("99", {"CODEC", "CODEC", "99", "113141", "bsbbbsbbbbs"})
code128_byid.Add("100", {"CODEB", "FNC4", "CODEB", "114131", "bsbbbbsbbbs"})
code128_byid.Add("101", {"FNC4", "CODEA", "CODEA", "311141", "bbbsbsbbbbs"})
code128_byid.Add("102", {"FNC1", "FNC1", "FNC1", "411131", "bbbbsbsbbbs"})
code128_byid.Add("103", {"StartA", "StartA", "StartA", "211412", "bbsbssssbss"})
code128_byid.Add("104", {"StartB", "StartB", "StartB", "211214", "bbsbssbssss"})
code128_byid.Add("105", {"StartC", "StartC", "StartC", "211232", "bbsbssbbbss"})
code128_byid.Add("106", {"Stop", "Stop", "Stop", "2331112", "bbsssbbbsbsbb"})
Return code128_byid
End Function
End Class
调用方法:
打印到picturebox
Dim final As String = clsBarcode.get_code_128Auto("这里写条码")
dim lineHeight as int16 = 50
For i = 0 To final.Length - 1
If (final.Substring(i, 1) = "b") Then
PictureBox1.CreateGraphics.FillRectangle(New SolidBrush(Color.Black), New Rectangle(x, y, 1, lineHeight)) '参数分别为 x坐标,y坐标,线宽(设置为1),条码高度。
End If
x += 1
Next i
Dim space As Int16 = final.Length / barcode.Length 'barcode 即为条码
x = 40 + 2
For i = 0 To barcode.Length - 1
PictureBox1.CreateGraphics.DrawString(barcode.Substring(i, 1), New Font("宋体", 6), New SolidBrush(Color.Black), x + i * space, y + lineHeight)
Next i
打印到打印机:
If barcode = "" Then 'barcode即为条码
MsgBox("选择条码")
Exit Sub
End If
gBarcode = barcode 'gbarcode为公有变量。
Printer.Print()
'请将上面的代码放到button click事件中。
'下面是打印机的打印页面事件,在这里画条码
Private Sub Printer_PrintPage(sender As Object, e As PrintPageEventArgs) Handles Printer.PrintPage
Const lineWidth = 1
Const lineHeight = 20
Dim x As Int16 = 40
Dim y As Int16 = 50
Dim final As String = ""
final = clsBarcode.get_code_128Auto(gBarcode)
For i = 1 To final.Length - 2
If (final.Substring(i, 1) = "b") Then
e.Graphics.FillRectangle(New SolidBrush(Color.Black), New Rectangle(x, y, lineWidth, lineHeight))
End If
x = x + 1
Next
End If
'把数字写上
Dim space As Int16 = final.Length / gBarcode.Length
x = 40 + 2
For i = 0 To gBarcode.Length - 1
e.Graphics.DrawString(gBarcode.Substring(i, 1), New Font("宋体", 6), New SolidBrush(Color.Black), x + i * space, y + lineHeight)
Next i
End Sub
上面举的例子是使用code128 auto来打印到图像框和打印机。其它类型的条码一样。类的工作是计算出哪个位置打线条,哪个位置不打。
code128 auto 打印出来的条码即短,又不受条码位数限制。而code128C只能打印偶数位的条码。
auto 即,如果是奇数,那么前偶数个用128C打,最后一位,先改变编码再打印。
具体规则,网上有,我就不搬了。
代码写得比较匆忙,没有整理。看官见谅。
有问题的可以在评论区指出。谢谢
可以直接下载完整示例
vb.net打印条形码code39、code128A、code128C、code128Auto(picturebox和打印机)-桌面系统文档类资源-CSDN下载已经封装成类。几句代码即可调用。支持code39码、code128A,code128C,code更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/superfans98/86269944