asp.net验证码的生成与刷新。。。。

来源:互联网 发布:如何将mac照片导入硬盘 编辑:程序博客网 时间:2024/06/06 08:14

 先看代码 这是生成验证码的。。

CreateCode.aspx.cs 

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace WebApplication1
{
public partial class CreateCode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Session["CheckCode"]=CreateCheckCodeImage();
}
private string CreateCheckCodeString()
{
char[] allCharArray = { '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' };//定义验证字符串
string randomcode = "";
Random rand = new Random();
for (int i = 0; i < 4; i++)
{
randomcode += allCharArray[rand.Next(allCharArray.Length)];
}//生成四位验证码
return randomcode;
}
/// <summary>
/// CreateCheckCodeImage(string sFilePath)创建验证码图片;
/// </summary>
/// <param name="sFilePath"></param>
public string CreateCheckCodeImage()
{


string checkcode = CreateCheckCodeString();//获取四位验证码
int iWidth = 55;//定义图片的宽度
int iHeight = 22;//定义图片的高度
Font font = new Font("Arial", 12, FontStyle.Bold);//定义大小为12pt的字体,用于绘制文字
SolidBrush brush = new SolidBrush(Color.Black);//定义黑色单笔画刷
Pen pen1 = new Pen(Color.Gray, 0);//定义画笔,用于绘制干扰线,横向干扰线
Pen pen2 = new Pen(Color.FromArgb(255, 100, 100, 100), 0);//纵向干扰线
Bitmap image = new Bitmap(iWidth, iHeight);//创建一个位图对象
Graphics g = Graphics.FromImage(image);//创建一个画图表面
g.Clear(ColorTranslator.FromHtml("#F0F0F0"));//在绘图表面填充颜色
RectangleF rect = new RectangleF(5, 2, iWidth, iHeight);//绘制文字的矩形区域
Random rand = new Random();//生成两条横向干扰线
for (int i = 0; i < 2; i++)
{
Point p1 = new Point(0, rand.Next(iHeight));
Point p2 = new Point(iWidth, rand.Next(iWidth));
g.DrawLine(pen1, p1, p2);


}

for (int i = 0; i < 4; i++)//生成四条纵向干扰线
{
Point p1 = new Point(rand.Next(iWidth), 0);
Point p2 = new Point(rand.Next(iWidth), iHeight);
g.DrawLine(pen2, p1, p2);
}
g.DrawString(checkcode, font, brush, rect);//绘制验证码文字
image.Save(Response.OutputStream, ImageFormat.Jpeg);//保存验证图像于输出流
Response.ContentType = "image/Jpeg";
g.Dispose();
image.Dispose();
return checkcode;//返回验证码,用于校验



}
}
}

--------------------------------------

下面我们看前台如何实现点击刷新

这里用的是html的控件。。。


<script type="text/javascript">
function refreshimg()
{
  var verify= document.getElementById("img"); //img为下面html控件的id
   verify.setAttribute('src','CreateCode.aspx?'+Math.random());
}
</script>

<style type="text/css">

</style>

<img id="img" src="CreateCode.aspx " alt="验证码"style="cursor: pointer;" onclick="this.src=this.src+'?'"><a href="javascript:refreshimg()" >看不清楚换一张</a>&nbsp;<br /><!--点击文本更换验证码-->

--------------------------

个人css一知半解。做的比较丑。这个点击图片可以更换,点击超链接也可以。其实重要的是javascript那个地方怎么写。。看来js还得好好学学。。之前喵了点。。。