' ***********************************************************************
' Module: ValidateCode.vb
' Author: Administrator
' Purpose: Definition of the Class ManageNotice
' ***********************************************************************
Option Strict Off
Imports Microsoft.VisualBasic
Imports System
Imports System.io
Imports System.drawing
Imports System.drawing.imaging
Namespace bbs.Web.BackGroundAdmin
Public Class ValidateCode
Inherits System.Web.UI.Page
#Region " Web 窗体设计器生成的代码 "
'该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents adminname As System.Web.UI.WebControls.TextBox
Protected WithEvents adminPwd As System.Web.UI.WebControls.TextBox
Protected WithEvents lblListValidateCode As System.Web.UI.WebControls.Label
Protected WithEvents txtValidateCode As System.Web.UI.WebControls.TextBox
'注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call list()
End Sub
Function list()
'在此处放置初始化页的用户代码
'rndnum是一个自定义函数
Dim vnum As String = rndnum(4)
Session("vnum") = vnum
validatecode(vnum)
End Function
'生成图象验证码函数
Sub validatecode(ByVal vnum)
Dim img As System.drawing.Bitmap
Dim g As Graphics
Dim r As Random = New Random
Dim gheight As Integer = Int(Len(vnum) * 13)
''gheight为图片宽度,根据字符长度自动更改图片宽度
img = New Bitmap(gheight, 20)
g = Graphics.FromImage(img)
'g.DrawString(vnum, New System.Drawing.Font("Arial", 10), New System.Drawing.SolidBrush(Color.Blue), 3, 3)
'新增,修改
'画图片的背景噪音线
For i As Integer = 0 To 25
Dim x1 As Integer
x1 = r.Next(img.Width)
Dim x2 As Integer = r.Next(img.Width)
Dim y1 As Integer = r.Next(img.Height)
Dim y2 As Integer = r.Next(img.Height)
g.DrawLine(New Pen(Color.Silver), x1, y1, x2, y2)
Next i
Dim font As font
font = New System.Drawing.Font("Arial", 12)
Dim brush As System.Drawing.Drawing2D.LinearGradientBrush
brush = New System.Drawing.Drawing2D.LinearGradientBrush(New Rectangle(0, 0, img.Width, img.Height), Color.Blue, Color.Blue, 1.2F, True)
g.DrawString(vnum, font, brush, 2, 2)
''画图片的前景噪音点
For ii As Integer = 0 To 100
Dim x As Integer = r.Next(img.Width)
Dim y As Integer = r.Next(img.Height)
img.SetPixel(x, y, Color.FromArgb(r.Next()))
Next
'画图片的边框线
g.DrawRectangle(New Pen(Color.Silver), 0, 0, img.Width - 1, img.Height - 1)
'在矩形内绘制字串(字串,字体,画笔颜色,左上x.左上y)
Dim ms1 As System.IO.MemoryStream
ms1 = New System.IO.MemoryStream
img.Save(ms1, System.Drawing.Imaging.ImageFormat.Png)
Response.ClearContent() '需要输出图象信息 要修改HTTP头
Response.ContentType = "image/Png"
Response.BinaryWrite(ms1.ToArray())
g.Dispose()
img.Dispose()
Response.End()
End Sub
'--------------------------------------------
'函数名称:rndnum
'函数参数:vcodenum--设定返回随机字符串的位数
'函数功能:产生数字和字符混合的随机字符串
Function rndnum(ByVal vcodenum)
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,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z"
Dim vcarray() As String = Split(vchar, ",") '将字符串生成数组
Dim vnum As String = ""
Dim i As Byte
For i = 1 To vcodenum
Randomize()
vnum = vnum & vcarray(Int(35 * Rnd())) '数组一般从0开始读取,所以这里为35*rnd
Next
Return vnum
End Function
End Class
End Namespace