Imports System.Drawing Public Class CreateImage
Public Shared Sub DrawImage() Dim img As New CreateImage() HttpContext.Current.Session("CheckCode") = img.RndNum(4) img.CreateImages(HttpContext.Current.Session("CheckCode").ToString()) End Sub 'DrawImage
'/ <summary> '/ 生成验证图片 '/ </summary> '/ <param name="checkCode">验证字符</param> Private Sub CreateImages(ByVal checkCode As String) Dim iwidth As Integer = CInt(checkCode.Length * 13) Dim image As New System.Drawing.Bitmap(iwidth, 23) Dim g As Graphics = Graphics.FromImage(image) g.Clear(Color.White) '定义颜色 Dim c As Color() = {Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple} '定义字体 Dim font As String() = {"Verdana", "Microsoft Sans Serif", "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) Next i
'输出不同字体和颜色的验证码字符 For i = 0 To checkCode.Length - 1 Dim cindex As Integer = rand.Next(7) Dim findex As Integer = rand.Next(5)
Dim f = New System.Drawing.Font(font(findex), 10, System.Drawing.FontStyle.Bold) Dim b = New System.Drawing.SolidBrush(c(cindex)) Dim ii As Integer = 4 If (i + 1) Mod 2 = 0 Then ii = 2 End If g.DrawString(checkCode.Substring(i, 1), f, b, 3 + i * 12, ii) Next i '画一个边框 g.DrawRectangle(New Pen(Color.Black, 0), 0, 0, image.Width - 1, image.Height - 1)
'输出到浏览器 Dim ms As New System.IO.MemoryStream() image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg) HttpContext.Current.Response.ClearContent() 'Response.ClearContent(); HttpContext.Current.Response.ContentType = "image/Jpeg" HttpContext.Current.Response.BinaryWrite(ms.ToArray()) g.Dispose() image.Dispose() End Sub 'CreateImages
'/ <summary> '/ 生成随机的字母 '/ </summary> '/ <param name="VcodeNum">生成字母的个数</param> '/ <returns>string</returns> Private Function RndNum(ByVal VcodeNum As Integer) As String Dim Vchar As String = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k" Dim VcArray As String() = Vchar.Split(","c) Dim VNum As String = "" '由于字符串很短,就不用StringBuilder了 Dim temp As Integer = -1 '记录上次随机数值,尽量避免生产几个一样的随机数 '采用一个简单的算法以保证生成随机数的不同 Dim rand As New Random() Dim i As Integer For i = 1 To (VcodeNum + 1) - 1 If temp <> -1 Then 'rand = New Random(i * temp * Convert.ToInt64(Int(DateTime.Now.Ticks))) rand = New Random(i * temp * 1000000) ' 'ToDo: Error processing original source shown below '-------------------------^--- GenCode(token): unexpected token type End If Dim t As Integer = rand.Next(VcArray.Length) If temp <> -1 And temp = t Then Return RndNum(VcodeNum) End If temp = t VNum += VcArray(t) Next i Return VNum End Function 'RndNum
|