前台:Demo.aspx:
<asp:Image id= "Image1 " runat= "server " ImageUrl= "gif.aspx " onclick= "this.src= 'gif.aspx ' " />
后台:Gif.aspx:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'RndNum为生成随机码的函数,
Dim VNum As String = RndNum(4) '该值为生成验证码的位数
Session( "VNum ") = VNum '读取Session
ValidateCode(VNum) '根据Session生成图片
End Sub
'--------------------------------------------
'生成图象验证码函数
Sub ValidateCode(ByVal VNum)
Dim Img As System.Drawing.Bitmap
Dim g As Graphics
Dim ms As MemoryStream
Dim gheight As Integer = Int(Len(VNum) * 14)
'gheight为图片宽度,根据字符长度自动更改图片宽度
Img = New Bitmap(gheight, 24)
g = Graphics.FromImage(Img)
'背景颜色
g.Clear(Color.Black)
g.DrawString(VNum, (New Font( "宋体 ", 16, FontStyle.Strikeout)), (New SolidBrush(Color.White)), 1, 3) '在矩形内绘制字串(字串,字体,画笔颜色,左上x.左上y)
ms = New MemoryStream()
Img.Save(ms, ImageFormat.Png)
Response.ClearContent() '需要输出图象信息 要修改HTTP头
Response.ContentType = "image/Png "
Response.BinaryWrite(ms.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 " '需要使用中文验证,可以修改这里和ValidateCode函数中的字体