Sto creando un programma per scrivere un nome in codici a barre (code39), per far ciò ho provato ad usare un font ma è troppo banale così ho deciso di provare a usare l'elemento Graphics.
Sono riuscito a farlo funzionare ma non so perchè ogni tanto fa le linee di dimensioni che vuole lui...
Cioè, se le dimensioni delle barre sono Grande=1 Piccola=0.5 l'applicazione ogni tanto fa la dimensione che vuole, oppure sbaglia la X, non capisco... Ecco il codice sorgente
(E' incasinato, tanto...)
Public BarCodes(122) As String
Private Const BARCODE_HEIGHT As Integer = 80
Private Const BARCODE_TOP As Integer = 10
Private Const BARCODE_LEFT As Integer = 10
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim barwidth As Single
barwidth = 9
Dim BinaryString As String = ""
Dim jmlText, jmlChar, i As Integer
Dim pen As New System.Drawing.Pen(System.Drawing.Color.Black, barwidth / 2)
Dim penB As New System.Drawing.Pen(System.Drawing.Color.Black, barwidth)
Dim PenW As New System.Drawing.Pen(System.Drawing.Color.White, barwidth)
Dim penwhite As New System.Drawing.Pen(System.Drawing.Color.White, barwidth / 2)
jmlChar = 0
jmlText = Len(TextBox1.Text)
For i = 0 To jmlText - 1
TextBox1.Focus()
TextBox1.SelectionStart = i
TextBox1.SelectionLength = 1
BinaryString = BinaryString & BarCodes(Asc(TextBox1.SelectedText)) & "0"
Next
Dim pb As PictureBox
Dim g As Graphics
pb = New PictureBox
SetCodes()
'Creating the Bar Code
With pb
.Width = 100
.Height = BARCODE_HEIGHT
pb.Image = New Bitmap(.Width, .Height)
End With
Try
g = Graphics.FromImage(pb.Image)
g.Clear(Color.White)
Dim X As Single = BARCODE_LEFT
Label1.Text = BinaryString
For i = 0 To Len(BinaryString) - 1
If i Mod 2 = 0 And 1 Then
If Mid(BinaryString, i + 1, 1) = "1" Then
g.DrawLine(penB, X, BARCODE_TOP, X, BARCODE_HEIGHT + BARCODE_TOP)
X = X + penB.Width
ElseIf Mid(BinaryString, i + 1, 1) = "0" Then
g.DrawLine(pen, X, BARCODE_TOP, X, BARCODE_HEIGHT + BARCODE_TOP)
X = X + pen.Width
End If
Else
If Mid(BinaryString, i + 1, 1) = "1" Then
g.DrawLine(PenW, X, BARCODE_TOP, X, BARCODE_HEIGHT + BARCODE_TOP)
X = X + PenW.Width
ElseIf Mid(BinaryString, i + 1, 1) = "0" Then
g.DrawLine(penwhite, X, BARCODE_TOP, X, BARCODE_HEIGHT + BARCODE_TOP)
X = X + penwhite.Width
End If
End If
Next
PictureBox1.Image = pb.Image
Catch se As Exception
MessageBox.Show(se.Message)
Finally
pb.Dispose()
g.Dispose()
PenW.Dispose()
pen.Dispose()
penB.Dispose()
penwhite.Dispose()
End Try
End Sub
Function SetCodes()
BarCodes(97) = "100001001"
BarCodes(98) = "001001001"
BarCodes(99) = "101001000"
BarCodes(100) = "000011001"
BarCodes(101) = "100011000"
BarCodes(102) = "001011000"
BarCodes(103) = "000001101"
BarCodes(104) = "100001100"
BarCodes(105) = "001001100"
BarCodes(106) = "000011100"
BarCodes(107) = "100000011"
BarCodes(108) = "001000011"
BarCodes(109) = "101000010"
BarCodes(110) = "000010011"
BarCodes(111) = "100010010"
BarCodes(112) = "001010010"
BarCodes(113) = "000000111"
BarCodes(114) = "100000110"
BarCodes(115) = "001000110"
BarCodes(116) = "000010110"
BarCodes(117) = "110000001"
BarCodes(118) = "011000001"
BarCodes(119) = "111000000"
BarCodes(120) = "010010001"
BarCodes(121) = "110010000"
BarCodes(122) = "011010000"
End Function
Le cifre binarie funzionano così:
si alternano tra nero-bianco (inizia il nero), 0 sta per linea piccola, 1 per linea grossa...
Sto creando un programma per scrivere un nome in codici a barre (code39), per far ciò ho provato ad usare un font ma è troppo banale così ho deciso di provare a usare l'elemento Graphics.
Sono riuscito a farlo funzionare ma non so perchè ogni tanto fa le linee di dimensioni che vuole lui...
Cioè, se le dimensioni delle barre sono Grande=1 Piccola=0.5 l'applicazione ogni tanto fa la dimensione che vuole, oppure sbaglia la X, non capisco... Ecco il codice sorgente
(E' incasinato, tanto...)
Le cifre binarie funzionano così:
si alternano tra nero-bianco (inizia il nero), 0 sta per linea piccola, 1 per linea grossa...
Ciao