主题:谁会写验证码乱写的效果 告诉一下 谢谢!
yzhzh1984
[专家分:0] 发布于 2007-10-21 16:53:00
最近做验证码 想要乱写的效果 在网上搜了几天都没搜到 只能求助各位高手
谁会写验证码乱写的效果 告诉一下 谢谢!
板凳
aaaliangjing [专家分:0] 发布于 2008-03-11 10:24:00
Private Sub CreateValidImage(ByVal checkCode As String)
Dim iWidth As Integer = checkCode.Length * 17
Dim image As New Bitmap(iWidth, 25) '图片大小
Dim g As Graphics = Graphics.FromImage(image)
g.Clear(Color.White)
'定义颜色
Dim c As Color() = {Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Red, Color.Brown, Color.DarkCyan, Color.Purple}
'定义字体
Dim font As String() = {"Verdana", "System", "Comic Sans MS", "Arial", "宋体"}
'随机输出噪点
Dim rand As New Random
Dim i As Integer
For i = 0 To 49
Dim x As Integer = rand.Next(image.Width)
Dim y As Integer = rand.Next(image.Height)
g.DrawRectangle(New Pen(Color.LightGray, 0), x, y, 1, 1) 'Pen(颜色,初始化宽度)画直线
Next
'输出不同字体和颜色的验证码字符
Dim j As Short
For j = 0 To checkCode.Length - 1
Dim cIndex As Short = rand.Next(8) '随机颜色
Dim fIndex As Short = rand.Next(5) '随机字体
Dim f As New Font(Font(fIndex), 12, FontStyle.Italic) '12磅,斜体
Dim b As New SolidBrush(c(cIndex)) '定义单色画刷
Dim PointY As Integer = 4
'If (j + 1) Mod 2 = 0 Then '每隔一个字符,让y坐标下移两个像素点
' PointY = 2
'End If
g.DrawString(checkCode.Substring(j, 1), f, b, 3 + (j * 14), PointY) '在矩形内绘制字串(字串,字体,画笔颜色,左上x.左上y)
Next
'画外框
g.DrawRectangle(New Pen(Color.Gray, 0), 0, 0, image.Width - 1, image.Height - 1)
'输出到浏览器
Dim msOutput As New System.IO.MemoryStream
image.Save(msOutput, Imaging.ImageFormat.Jpeg)
HttpContext.Current.Response.ClearContent() '需要输出图象信息 要修改HTTP头
HttpContext.Current.Response.ContentType = "image/Jpeg"
HttpContext.Current.Response.BinaryWrite(msOutput.ToArray())
'释放
g.Dispose()
image.Dispose()
End Sub
'生成随机字母或数字组合
'参数:VCodeNum 生成随机字母或数字的个数
Private Function RndNum(ByVal VCodeNum As Short) As String
Dim VChar As String
VChar = "0,1,2,3,4,5,6,7,8,9," & _
"A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,中,文,也,可,以"
Dim VCharArray As String() = VChar.Split(",") '将字符串生成数组
Dim ArrayLength As Integer = VCharArray.Length '数组长度
Dim rand As New Random
Dim rndInt As Integer '随机数
Dim CodeNum As String = "" '用于返回结果
Dim i As Integer
For i = 0 To VCodeNum - 1
rndInt = rand.Next(ArrayLength)
CodeNum &= VCharArray(rndInt)
Next
Return CodeNum
End Function