【代码块】-验证码-绘制到图片显示
整理代码块
代码块整理后存储,供后期使用
Sub showRandomStr()
str = String.Empty
' 生成随机数字符串
Dim r As Random = New Random()
For i = 0 To 4
str += r.Next(0, 9).ToString
Next
'画字符串
Dim bmp As Bitmap = New Bitmap(120, 20)
Dim g As Graphics = Graphics.FromImage(bmp)
Dim fonts As String() = {"宋体", "楷体", "仿宋", "草书", "华文行楷"}
Dim colors As Color() = {Color.Blue, Color.Green, Color.Red, Color.White, Color.Yellow}
For i = 0 To str.Length - 1
Dim p As Point = New Point(i * 20 + 10, 0)
g.DrawString(str(i).ToString, New Font(fonts(r.Next(0, fonts.Length)), 20), New SolidBrush(colors(r.Next(0, colors.Length))), p)
Next
'画线
For i = 0 To 20
g.DrawLine(New Pen(Color.Black), New Point(r.Next(0, bmp.Width), r.Next(bmp.Height)) _ , New Point(r.Next(0, bmp.Width), r.Next(bmp.Height)))
Next
'画点
For i = 0 To 300
bmp.SetPixel(r.Next(0, bmp.Width), r.Next(0, bmp.Height), Color.Black)
Next
pbx.Image = bmp
End Sub
如果有错误的地方,还望各位多多指点
写个博客,来记录自己成长的一些经历,或许也能顺便帮助他人。
由于使用GitHub仓库作为图床,会有图片显示不出来的情况。