How to draw and print Barcodes with one routine.

  • Thread starter Thread starter tommytwotrain
  • Start date Start date
T

tommytwotrain

Guest
Hi Everybody!

I made this barcode routine for another thread and thought I would post it with a better related subject name and for discussion.

The example uses the RenderScene subroutine to draw on picturebox or printer. You can change the scalefactor as req'd.

Any comments on the barcode drawing routine etc?

Its an example for ideas. One has to adapt it for ones use.

To make the example add a picturebox and a button to a form copy and paste both of these class sections into the form.

Click the Print button on the Preview Window to print on the default printer.

The printpreview image shows the scene enlarged on the default 8.5 x 11" paper.

1355225.png

'print and show barcode one routine v2
Public Class Form1
Private DrawBar As New code39
Private preview As New PrintPreviewDialog
Private pd As New System.Drawing.Printing.PrintDocument
Private HeadingText As String = "tommytwotrain"
Private barCodeText As String = "012345678901"

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ClientSize = New Size(440, 300)
PictureBox1.ClientSize = New Size(400, 200)
PictureBox1.Location = New Point(20, 20)

AddHandler pd.PrintPage, AddressOf OnPrintPage
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'show barcode in print preview
pd.DefaultPageSettings.Landscape = True
preview.Document = pd
preview.ShowDialog()
End Sub

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
'draw the barcode scene on the picturebox
e.Graphics.Clear(Color.White)
RenderScene(e.Graphics, 3, HeadingText, barCodeText)
End Sub

Private Sub OnPrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
'draw the barcode scene on the printer
RenderScene(e.Graphics, 6, HeadingText, barCodeText)
End Sub

Private Sub RenderScene(g As Graphics, sizeFactor As Double, theHeadingText As String, barCodeText As String)
'draw the barcode on a graphics surface g
'draw the bar code on a bitmap
Dim barcode1 As Bitmap = DrawBar.Generate(barCodeText, sizeFactor)
Dim x As Single = CSng(6 * sizeFactor)
'draw some text
g.DrawString(theHeadingText, New Font("tahoma", CSng(sizeFactor * 8), FontStyle.Bold), Brushes.Black, 2 * x, CInt(5 * sizeFactor))
'draw the barcode bitmap
g.DrawImage(barcode1, x, CInt(20 * sizeFactor))

barcode1.Dispose()
End Sub
End Class

'code39 class from How to generate Code39 barcodes in vb.net
'modified 'ttt 11-2-18

Public Class code39
Private bitsCode As ArrayList

Public Sub New()
bitsCode = New ArrayList
bitsCode.Add(New String(3) {"0001101", "0100111", "1110010", "000000"})
bitsCode.Add(New String(3) {"0011001", "0110011", "1100110", "001011"})
bitsCode.Add(New String(3) {"0010011", "0011011", "1101100", "001101"})
bitsCode.Add(New String(3) {"0111101", "0100001", "1000010", "001110"})
bitsCode.Add(New String(3) {"0100011", "0011101", "1011100", "010011"})
bitsCode.Add(New String(3) {"0110001", "0111001", "1001110", "011001"})
bitsCode.Add(New String(3) {"0101111", "0000101", "1010000", "011100"})
bitsCode.Add(New String(3) {"0111011", "0010001", "1000100", "010101"})
bitsCode.Add(New String(3) {"0110111", "0001001", "1001000", "010110"})
bitsCode.Add(New String(3) {"0001011", "0010111", "1110100", "011010"})
End Sub

Public Function Generate(ByVal Code As String, ByVal sizeFactor As Double) As Image
Dim a As Integer = 0
Dim b As Integer = 0
Dim imgCode As Image
Dim g As Graphics
Dim i As Integer
Dim bCode As Byte()
Dim bitCode As Byte()
Dim tmpFont As Font

If Code.Length <> 12 Or Not IsNumeric(Code.Replace(".", "_").Replace(",", "_")) Then Throw New Exception("Le code doit être composé de 12 chiffres")

ReDim bCode(12)
For i = 0 To 11
bCode(i) = CInt(Code.Substring(i, 1))
If (i Mod 2) = 1 Then
b += bCode(i)
Else
a += bCode(i)
End If
Next

i = (a + (b * 3)) Mod 10
If i = 0 Then
bCode(12) = 0
Else
bCode(12) = 10 - i
End If
bitCode = getBits(bCode)

tmpFont = New Font("times new roman", 14, FontStyle.Regular, GraphicsUnit.Pixel)

'ttt 11-2-18
'imgCode = New Bitmap(110, 50)
imgCode = New Bitmap(CInt(sizeFactor * 110), CInt(sizeFactor * 50))

g = Graphics.FromImage(imgCode)
g.Clear(Color.White)
'ttt 11-2-18
g.ScaleTransform(sizeFactor, sizeFactor)
g.DrawString(Code.Substring(0, 1), tmpFont, Brushes.Black, 2, 30)

a = g.MeasureString(Code.Substring(0, 1), tmpFont).Width

For i = 0 To bitCode.Length - 1
If i = 2 Then
g.DrawString(Code.Substring(1, 6), tmpFont, Brushes.Black, a, 30)
ElseIf i = 48 Then
g.DrawString(Code.Substring(7, 5) & bCode(12).ToString, tmpFont, Brushes.Black, a, 30)
End If

If i = 0 Or i = 2 Or i = 46 Or i = 48 Or i = 92 Or i = 94 Then
If bitCode(i) = 1 Then 'noir
g.DrawLine(Pens.Black, a, 0, a, 40)
a += 1
End If
Else
If bitCode(i) = 1 Then 'noir
g.DrawLine(Pens.Black, a, 0, a, 30)
a += 1
Else 'blanc
a += 1
End If
End If
Next
g.Flush()
Return imgCode
End Function

Private Function getBits(ByVal bCode As Byte()) As Byte()
Dim i As Integer
Dim res As Byte()
Dim bits As String = "101"
Dim cle As String = bitsCode(bCode(0))(3)
For i = 1 To 6
bits &= bitsCode(bCode(i))(CInt(cle.Substring(i - 1, 1)))
Next
bits &= "01010"
For i = 7 To 12
bits &= bitsCode(bCode(i))(2)
Next
bits += "101"
ReDim res(bits.Length - 1)
For i = 0 To bits.Length - 1
res(i) = Asc(bits.Chars(i)) - 48
Next
Return res
End Function
End Class

Continue reading...
 
Back
Top