码迷,mamicode.com
首页 > 其他好文 > 详细

登陆带验证码

时间:2017-12-29 15:17:28      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:xtu   color   cli   void   成功   password   画布   画图   submit   

<script>--脚本代码
function getVCode() {
var vcode = document.getElementById("vcode");--获取元素
vcode.src = "ValidateCode.ashx?i="+Math.random();
}
</script>


<form id="form1" runat="server">
<div id="login">
<div class="title">用户登录</div>
<div class="content">
<p>用户名:<input type="text" name="txtuser" class="txt" /></p>
<p>密&nbsp;&nbsp;&nbsp;码:<input type="password" name="txtpwd" class="txt" /></p>

<p>验证码:<input type="text" name="txtvcode" class="txt" />
<img src="ValidateCode.ashx" id="vcode" align="middle" alt="看不清,换一张" onclick="getVCode()" /><%--后台生成的图片--%>
<a href="javascript:getVCode()" class="hyz">换一张</a>
</p>

<p><input type="submit" class="sub" value="登录" /></p>
</div>
</div>
</form>

 

 

using System.Drawing;
using System.Text;
using System.IO;
using System.Drawing.Drawing2D;
using System.Web.SessionState;

public class ValidateCode : IHttpHandler,IRequiresSessionState
{

public void ProcessRequest(HttpContext context)
{
Random random = new Random();
context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
//1.创建画板或画布
Bitmap bitmap = new Bitmap(60, 30);
//2.创建画图工具
Graphics g = Graphics.FromImage(bitmap);

//设置画布背景颜色
g.Clear(Color.White);
//设置边框
g.DrawRectangle(new Pen(Color.Red), 0, 0, 59, 29);
//设置像干扰素点,噪音点
for (int i = 0; i < 100; i++)
{
int x1 = random.Next(bitmap.Width);
int y1 = random.Next(bitmap.Height);
bitmap.SetPixel(x1, y1, Color.Brown);
}
//设置干扰折线,噪音线
for (int i = 0; i < 25; i++)
{
int x1 = random.Next(bitmap.Width);
int x2 = random.Next(bitmap.Width);
int y1 = random.Next(bitmap.Height);
int y2 = random.Next(bitmap.Height);
g.DrawLine(new Pen(Color.Green), x1, y1, x2, y2);
}
//3.设置画图内容
string s = "0123456789abcdefghjkmnpkrstuvwxyz";
StringBuilder builder = new StringBuilder();

for (int i = 0; i < 4; i++)
{
int index= random.Next(s.Length);
builder.Append(s[index]);
}
context.Session["vcode"] = builder;
//4.画图

g.DrawString(builder.ToString(), new Font("宋体", 18),new LinearGradientBrush(new Rectangle(0,0,bitmap.Width,bitmap.Height),Color.Blue,Color.Red,1.2f),0,0);

//5.保存
//string path=context.Request.MapPath("image");
//bitmap.Save(path + "/" + "11.jpg");
//context.Response.Write("生成成功!");
bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}

public bool IsReusable
{
get
{
return false;
}
}
}

登陆带验证码

标签:xtu   color   cli   void   成功   password   画布   画图   submit   

原文地址:https://www.cnblogs.com/ZkbFighting/p/8143633.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!