45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:ASP.NET图片验证码大全

ASP.NET图片验证码大全

2016-09-09 09:22:59 来源:www.45fan.com 【

ASP.NET图片验证码大全

ASP.NET(VB) 图片验证码
Jerry(
k613@21cn.com)

根据网友的C#.NET图片验证改为VB.NET而已,由于VB.NET不支持unchecked,故去掉了按时间种子生成随机数的函

数部分。测试中并无出现相同的验证码。


ValidCode.vb
--------------
Imports System
Imports System.Drawing
Imports System.Web
Imports System.Web.UI

Public Class ValidCode
Inherits Page

'输入验证图片
Public Sub DrawImage()
Dim theCode As String
theCode = RndNum(4)
HttpContext.Current.Session("ValidCode") = theCode '保存验证码到Session,以便验证。
CreateValidImage(theCode)
End Sub

'生成验证图片
'参数:checkCode 验证字符
Private Sub CreateValidImage(checkCode As String)
Dim iWidth As Integer = checkCode.Length * 15
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)
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.Regular) '12磅,斜体
Dim b As New SolidBrush(c(cIndex)) '定义单色画刷

Dim PointY As Integer = 4
If (j+1) Mod 2 = 0 Then
PointY = 2
End If
g.DrawString(checkCode.SubString(j,1), f, b, 3+(j*13), PointY)
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()
HttpContext.Current.Response.ContentType = "image/Jpeg"
HttpContext.Current.Response.BinaryWrite(msOutput.ToArray())

'释放
g.Dispose()
image.Dispose()
End Sub

'生成随机字母或数字组合
'参数:VCodeNum 生成随机字母或数字的个数
Private Function RndNum(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," & _
"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

End Class
------------

ImageCode.aspx
<%@ Page Language="VB" Inherits="ValidCode" Src="ValidCode.vb" %>
<script runat="server">
Sub Page_Load()
Dim imgCode As New ValidCode
imgCode.DrawImage()
End Sub
</script>
------------

Login.aspx
<script language="vb" runat="server">
Sub ChangeCode_Click(s as Object, e as EventArgs)
ImageCode.DataBind()
End Sub
</script>
<body><form runat="server">
<p><asp:TextBox ID="code" runat="server" />
<asp:Image ID="ImageCode" ImageUrl="ImageCode.aspx" runat="server" />
<p><asp:LinkButton ID="ChangeCode" Text="看不清楚?换一个验证码" OnClick="ChangeCode_Click"

runat="server" />
</form>
</body>
-----------

 

本文地址:http://www.45fan.com/a/question/73982.html
Tags: 验证 图片 ASP.NET
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部