'不重復數字 PublicFunction ran2()Function ran2(ByVal num As Int16) AsString Dim s AsString="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z" Dim sz AsString() = s.Split(","c) Dim al =New ArrayList() Dim i AsInteger For i =0To num -1 If al.Count =0Then Dim rd =New Random() Dim a AsInteger= rd.Next(0, 35) al.Add(a) Else Dim c AsInteger=0 Dim isonly AsBoolean=True While isonly Dim rd =New Random() Dim b AsInteger= rd.Next(0, 35) IfMe.isone(al, b) Then'判断唯一的函数 isonly =False c = b EndIf EndWhile al.Add(c) EndIf Next i Dim rand AsString="" Dim j AsInteger For j =0To al.Count -1 rand += sz(Int32.Parse(al(j).ToString())) Next j Return rand End Function'ran2 '下面还有一个判断唯一的函数 PrivateFunction isone()Function isone(ByVal a As System.Collections.ArrayList, ByVal b AsInteger) AsBoolean Dim one AsBoolean=True Dim i AsInteger For i =0To a.Count -1 If a(i).ToString() = b.ToString() Then one =False i = a.Count EndIf Next i Return one End Function'isone
two:
'大寫小寫數字 PublicFunction RandomPasswd()Function RandomPasswd() AsString Dim strPasswd AsString="" Dim intNum AsInteger=0 Dim objRandom As Random Dim intLength AsInteger objRandom =New Random intLength = objRandom.Next(5) +8 For i AsInteger=0To intLength -1 intNum = objRandom.Next(3) If intNum =0Then strPasswd +=Chr(objRandom.Next(10) +48).ToString() ElseIf intNum =1Then strPasswd +=Chr(objRandom.Next(26) +65).ToString() Else strPasswd +=Chr(objRandom.Next(26) +97).ToString() EndIf Next Return strPasswd End Function
將隨機數以image顯示: one:增加噪音等安全系數
PrivateSub CreateCheckCodeImage()Sub CreateCheckCodeImage(ByVal checkCode AsString) If checkCode IsNothingOr checkCode.Trim() = [String].Empty Then Return EndIf Dim image AsNew System.Drawing.Bitmap(CInt(Math.Ceiling((checkCode.Length *15.5))), 30) Dim g As Graphics = Graphics.FromImage(image) Try '生成随机生成器 Dim random AsNew Random() '清空图片背景色 g.Clear(Color.White) '画图片的背景噪音线 Dim i AsInteger For i =0To24 Dim x1 AsInteger= random.Next(image.Width) Dim x2 AsInteger= random.Next(image.Width) Dim y1 AsInteger= random.Next(image.Height) Dim y2 AsInteger= random.Next(image.Height) g.DrawLine(New Pen(Color.Silver), x1, y1, x2, y2) Next i Dim font =New System.Drawing.Font("Arial", 14, System.Drawing.FontStyle.Bold Or System.Drawing.FontStyle.Italic) Dim brush AsNew System.Drawing.Drawing2D.LinearGradientBrush(New Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2F, True) g.DrawString(checkCode, font, brush, 2, 2) '画图片的前景噪音点 ' Dim i As Integer For i =0To99 Dim x AsInteger= random.Next(image.Width) Dim y AsInteger= random.Next(image.Height) image.SetPixel(x, y, Color.FromArgb(random.Next())) Next i '画图片的边框线 g.DrawRectangle(New Pen(Color.Silver), 0, 0, image.Width -1, image.Height -1) Dim ms AsNew System.IO.MemoryStream() image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif) Response.ClearContent() Response.ContentType ="image/Gif" Response.BinaryWrite(ms.ToArray()) Finally g.Dispose() image.Dispose() EndTry End Sub'
two:無噪音,正常圖片輸出
PrivateSub ValidateCode()Sub ValidateCode(ByVal VNum AsString) Dim Img As Bitmap =Nothing Dim g As Graphics =Nothing Dim ms As MemoryStream =Nothing Dim gheight AsInteger= VNum.Length *15 Img =New Bitmap(gheight, 30) g = Graphics.FromImage(Img) '背景颜色 g.Clear(Color.White) '文字字体 Dim f AsNew Font("Arial Black", 11) '文字颜色 Dim s AsNew SolidBrush(Color.Black) g.DrawString(VNum, f, s, 3, 3) ms =New MemoryStream() Img.Save(ms, ImageFormat.Jpeg) Response.ClearContent() Response.ContentType ="image/Jpeg" Response.BinaryWrite(ms.ToArray()) g.Dispose() Img.Dispose() Response.End() End Sub