//一般处理程序。注意接口。命名空间using System.Web.SessionState;//添加命名空间,用于保存session IRequiresSessionState
using System;
using System.Collections.Generic;using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Web;
using System.Web.SessionState;//添加命名空间,用于保存session
namespace LiBangWuLiu
{
/// <summary>
/// VerifyCode 的摘要说明
/// </summary>
public class VerifyCode : IHttpHandler,IRequiresSessionState //实现IRequiresSessionState
{
/// <summary>
/// 创建一副图片,画上验证码
/// </summary>
/// <param name="context"></param>
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/plain";//响应类型为图片image
string validateCode = getValidateCode(4);//获取一个四位数字的验证码
context.Session["validateCode"] = validateCode;//将验证码存到session中,
//创建验证码图片
using (Bitmap img = new Bitmap(60, 28))//创建具体位置图像,位图
{
using (Graphics g = Graphics.FromImage(img))//从指定的img创建新的图面)
{
g.Clear(Color.LightYellow);//初始化图片背景色
g.DrawRectangle(new Pen(Color.Red), 0, 0, img.Width - 1, img.Height - 1);//画图片上矩形的边框,从坐标(0,0)开始,相对于图片的坐标
Font font = new System.Drawing.Font("Arial", 16, (System.Drawing.FontStyle.Bold));//字体样式
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Blue, Color.DarkRed, 1.2F, true);//渐变效果
g.DrawString(validateCode, font, brush, 2, 2);//将验证码写入图片
//画前景线 ,随机生成几个点,(x1,y1)到(x2,y2)画线
Random ran = new Random();
int i;
for (i = 0; i < 10; i++)
{
int x1 = ran.Next(img.Width);
int x2 = ran.Next(img.Width);
int y1 = ran.Next(img.Height);
int y2 = ran.Next(img.Height);
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
}
img.Save(context.Response.OutputStream, ImageFormat.Jpeg);//将图片保存到响应流
}
}
/// <summary>
/// 生成随机码的方法,
/// </summary>
/// <param name="validateCodeLength">随机码的长度</param>
/// <returns>随机码</returns>
private string getValidateCode(int validateCodeLength)
{
string allchar =@"123456789qwertyuioplkjhgfdsazxcvbnm123456789QWERTYUIOPLKJHGFDSAZXCVBNM";//列出所有可能出现的字符
int length = allchar.Length;//获取所有字符的个数
Random ran = new Random();//声明生成一个随机数的对象
string validateCode = string.Empty;//声明随机码
for (int i = 0; i < validateCodeLength; i++)
{
validateCode += allchar[ran.Next(length)];//找到随机码,用生成的随机数作为字符串的索引,下标
}
return validateCode;
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
//前台页面FORM提交,写上runat="server"才会有提交数据,标签是不能提交数据的,只有控件才可以,并且要runat="server",标签可以JS注册事件validateCode_img是ID
<script type="text/javascript">
function changeImage() {
document.getElementById("validateCode_img").src = document.getElementById("validateCode_img").src + '?';
}
</script>
<form action="reg.aspx" method="post" id="registForm" runat="server"><%--runat="server"可以使用下边的服务器控件--%>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td width="150"><label class="tit"><span class="f_f15a22">*</span>手机号:</label></td>
<td width="212"><input id="entity.ebccMobile" name="mobile" type="text" ov="请输入11位手机号码" maxlength="11" value="请输入11位手机号码" class="input w205 inputFocus grays"></td>
<td><div class="tips" id="phone_tips"></div></td>
</tr>
<tr>
<td><label class="tit"><span class="f_f15a22">*</span>密码:</label></td>
<td><input id="entity.ebccNetPassword" name="entity.ebccNetPassword" type="password" class="input w205 inputFocus grays"></td>
<td><div class="tips" id="password_tips"></div></td>
</tr>
<tr>
<td><label class="tit"><span class="f_f15a22">*</span>确认密码:</label></td>
<td><input id="password2" name="password2" type="password" class="input w205 inputFocus grays"></td>
<td><div class="tips" id="password2_tips"></div></td>
</tr>
<tr>
<td><label class="tit">用户名:</label></td>
<td><input id="entity.ebccNetLogin" name="loginName" type="text" ov="可作为登录账号" value="可作为登录账号" class="input w205 inputFocus grays"></td>
<td><div class="tips" id="userName_tips"></div></td>
</tr>
<tr>
<td><label class="tit">邮箱地址:</label></td>
<td><input id="entity.ebccEmail" name="email" type="text" class="input w205 inputFocus grays" ov="可作为登录账号和密码找回凭证" value="可作为登录账号和密码找回凭证"></td>
<td><div class="tips" id="email_tips"></div></td>
</tr>
<tr>
<td><label class="tit"><span class="f_f15a22">*</span>验证码:</label></td>
<td><input id="validateCode_regist" name="validateCode_regist" type="text" class="input w95 inputFocus grays" value="请输入验证码" ov="请输入验证码">
<span> <img id="validateCode_img" src="/handerashx/VerifyCode.ashx" width="60" height="28" style="vertical-align: middle;"/> <a id="valida" href="javascript:changeImage()">换一组</a> </span></td>
<td><div class="tips" id="validCode_msg" style="display:none;">
<p class="erro"><span class="icon_del"></span>请输入正确验证码</p>
</div></td>
</tr>
<tr>
<td><label class="tit"><span class="f_f15a22">*</span>短信验证码:</label></td>
<td><input id="registVerCode" name="registVerCode" type="text" class="input w95 inputFocus grays" value="请输入验证码" ov="请输入验证码">
<input type="button" id="getPhoneVerCodeBtn" class="xz1" style="padding:0 10px;" value="获取验证码" οnclick="sendPhoneVerCode()"></td>
<td><div class="tips" id="verCode_tips"></div></td>
</tr>
<tr>
<td> </td>
<td><input id="accept" name="accept" type="checkbox" checked="checked">
已阅读<a href="javascript:void(0);" class="f_f15a22" id="user_agreement">礼邦物流网上服务协议</a></td>
</tr>
<tr>
<td> </td>
<%--<td><a href="javascript:regist();" class="xz1">立即注册</a></td>--%>
<td><%--<input type="submit" id="tijiao" value="提交" class="xz1" οnclick="tijiaoClick" />--%>
<asp:Button ID="submit" runat="server" Text="立即注册" CssClass="xz1" OnClick="submit_Click" />
</td>
<%-- <td><input runat="server" type="button" id="button1" name="zhuce" class="xz1" />立即注册</td>--%>
</tr>