我一直在研究像thigny这样的条形码。它使用颜色将数字写入图像。无论如何,每次我使用超过9个数字时,它都表示OverFlowException未处理。也许我在代码中遗漏了一些东西。大多数是我的代码,除了关于TextBox1 KeyPress的代码。这是代码
Public Class Form1
Dim NumberLength As Integer = 1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If Button2.Text = "..." Then
MsgBox("Place select where you want your Color Code to be saved")
Else
Dim BMP As New Drawing.Bitmap(19, 10)
BMP = Create(TextBox1.Text)
Dim NewBMP As New Drawing.Bitmap(NumberLength, 1)
NewBMP = BMP
NewBMP.Save(Button2.Text)
End If
End Sub
Public Function Create(ByVal Number As Int32) As Bitmap
Dim StringNumber = Number.ToString
Dim NumberLength = StringNumber.Length
Dim ColorCode As New Drawing.Bitmap((NumberLength), 1)
Dim GFX As Graphics = Graphics.FromImage(ColorCode)
For i As Integer = 1 To NumberLength
Dim CurrentNumber As Integer = StringNumber.Substring(i - 1, 1)
GFX.FillRectangle(New SolidBrush(ReturnRGBValue(CurrentNumber)), i, 0, 1, 1)
If i = (NumberLength + 1) Then
Exit For
End If
Next
Return ColorCode
End Function
Public Function ReturnRGBValue(ByVal Number As Integer) As Color
Dim Color As New Color
If Number = 1 Then
Color = Color.FromArgb(255, 255, 255)
ElseIf Number = 2 Then
Color = Color.FromArgb(245, 245, 245)
ElseIf Number = 3 Then
Color = Color.FromArgb(235, 235, 235)
ElseIf Number = 4 Then
Color = Color.FromArgb(225, 225, 225)
ElseIf Number = 5 Then
Color = Color.FromArgb(215, 215, 215)
ElseIf Number = 6 Then
Color = Color.FromArgb(205, 205, 205)
ElseIf Number = 7 Then
Color = Color.FromArgb(195, 195, 195)
ElseIf Number = 8 Then
Color = Color.FromArgb(185, 185, 185)
ElseIf Number = 9 Then
Color = Color.FromArgb(175, 175, 175)
ElseIf Number = 0 Then
Color = Color.FromArgb(165, 165, 165)
End If
Return Color
End Function
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim Save As New SaveFileDialog
Save.Filter = "PNG |*.png |JPG |*.jpg |GIF |*.gif"
Save.ShowDialog()
Button2.Text = Save.FileName
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
'97 - 122 = Ascii codes for simple letters
'65 - 90 = Ascii codes for capital letters
'48 - 57 = Ascii codes for numbers
If Asc(e.KeyChar) <> 8 Then
If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
e.Handled = True
End If
End If
End Sub
End Class
答案 0 :(得分:2)
The valid range of an Integer variable is -2147483648 through +2147483647.
Dim k As Integer
The following statement causes an error because the value is too large.
k = 2147483648
您正在使用Integer,因此如果它存在9,则溢出,请尝试使用Long
数据类型see tihs。
答案 1 :(得分:1)
您的论点被声明为 Int32
可以存储在Int32中的最大值(根据MSDN)十进制 2,147,483,647 (十六进制值 0x7FFFFFFF
)