Friend Class cUPCA
' Methods
Public Function CreateBarCode(ByVal txt As String, ByVal scale As Integer) As Image
Dim img As Image = Nothing
Me.imageWidth = 120
Me.imageScale = scale
Me.imageWidth = Convert.ToInt32(CSng((Me.imageWidth * Me.imageScale)))
Dim imageHeightHolder As Integer = Convert.ToInt32(CSng((Me.barCodeHeight * Me.imageScale)))
Dim incomingString As String = txt.ToUpper
If (incomingString.Length = 0) Then
Return img
End If
Dim length As Integer = incomingString.Length
Me.newBitmap = New Bitmap(Me.imageWidth, imageHeightHolder, PixelFormat.Format32bppArgb)
Me.g = Graphics.FromImage(Me.newBitmap)
Me.g.ScaleTransform(Me.imageScale, Me.imageScale)
Dim newRec As New Rectangle(0, 0, Me.imageWidth, imageHeightHolder)
Me.g.FillRectangle(New SolidBrush(Color.White), newRec)
Me.placeMarker = 0
txt = (txt.Substring(0, 11) & Me.GetCheckSum(txt).ToString)
Dim wholeSet As Integer = 0
wholeSet = 1
Do While (wholeSet <= Convert.ToInt32(incomingString.Length))
Dim currentASCII As Integer = (Convert.ToChar(incomingString.Substring((wholeSet - 1), 1)) - "0"c)
If (wholeSet = 1) Then
Me.DrawSet(Me.UPCABegin, Me.placeMarker, 0)
Me.DrawSet(Me.UPCALeft(currentASCII), Me.placeMarker, 0)
ElseIf (wholeSet <= 5) Then
Me.DrawSet(Me.UPCALeft(currentASCII), Me.placeMarker, 6)
ElseIf (wholeSet = 6) Then
Me.DrawSet(Me.UPCALeft(currentASCII), Me.placeMarker, 6)
Me.DrawSet(Me.UPCAMiddle, Me.placeMarker, 0)
ElseIf (wholeSet <= 11) Then
Me.DrawSet(Me.UPCARight(currentASCII), Me.placeMarker, 6)
ElseIf (wholeSet = 12) Then
Me.DrawSet(Me.UPCARight(currentASCII), Me.placeMarker, 0)
Me.DrawSet(Me.UPCAEnd, Me.placeMarker, 0)
End If
wholeSet += 1
Loop
Using font As Font = New Font("Courier New, Bold", 9!)
Me.g.DrawString(txt.Substring(0, 1), font, Brushes.Black, New PointF(0!, 67!))
Me.g.DrawString(txt.Substring(1, 5), font, Brushes.Black, New PointF(22!, 67!))
Me.g.DrawString(txt.Substring(6, 5), font, Brushes.Black, New PointF(60!, 67!))
Me.g.DrawString(txt.Substring(11, 1), font, Brushes.Black, New PointF(108!, 67!))
End Using
Return Image.FromHbitmap(Me.newBitmap.GetHbitmap)
End Function
Private Sub DrawSet(ByVal upcCode As String, ByVal drawLocation As Integer, ByVal barHeight As Integer)
Dim currentLetterArray As Integer() = New Integer(upcCode.Length - 1) {}
Me.placeMarker = (Me.placeMarker + upcCode.Length)
barHeight = (Me.barCodeHeight - barHeight)
Dim i As Integer = 0
i = 0
Do While (i <= (upcCode.Length - 1))
currentLetterArray(i) = Convert.ToInt16(upcCode.Substring(i, 1))
i += 1
Loop
i = 0
Do While (i <= (upcCode.Length - 1))
If (currentLetterArray(i) = 0) Then
Me.g.DrawLine(Pens.White, (i + drawLocation), 0, (i + drawLocation), (barHeight - 6))
ElseIf (currentLetterArray(i) = 1) Then
Me.g.DrawLine(Pens.Black, (i + drawLocation), 0, (i + drawLocation), (barHeight - 6))
End If
i += 1
Loop
End Sub
Public Function GetCheckSum(ByVal barCode As String) As Integer
Dim leftSideOfBarCode As String = barCode.Substring(0, 11)
Dim total As Integer = 0
Dim currentDigit As Integer = 0
Dim i As Integer = 0
i = 0
Do While (i <= (leftSideOfBarCode.Length - 1))
currentDigit = Convert.ToInt32(leftSideOfBarCode.Substring(i, 1))
If (((i - 1) Mod 2) = 0) Then
total = (total + currentDigit)
Else
total = (total + (currentDigit * 3))
End If
i += 1
Loop
Return ((10 - (total Mod 10)) Mod 10)
End Function
' Fields
Private barCodeHeight As Integer = 80
Private g As Graphics
Private imageScale As Single = 1!
Private imageWidth As Integer
Private newBitmap As Bitmap
Private placeMarker As Integer
Private UPCABegin As String = "0000000000000101"
Private UPCAEnd As String = "1010000000000000"
Private UPCALeft As String() = New String() { "0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011" }
Private UPCAMiddle As String = "01010"
Private UPCARight As String() = New String() { "1110010", "1100110", "1101100", "1000010", "1011100", "1001110", "1010000", "1000100", "1001000", "1110100" }
End Class