62,046
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Web.UI;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing.Imaging;
/// <summary>
///ValidateNumber 生成验证码
/// </summary>
public class ValidateNumber
{
//产生验证码的字符集 (易混淆的字符去掉)
private string charcode = "2,3,4,5,6,8,9,A,B,C,D,E,F,G,H,J,K,M,N,P,R,S,U,W,X,Y,a,b,c,d,e,f,g,h,j,k,m,n,p,r,s,u,w,x,y";
/// <summary>
/// 验证码的最大长度
/// </summary>
public int MaxLength
{
get { return 10; }
}
/// <summary>
/// 验证码的最小长度
/// </summary>
public int MinLength
{
get { return 1; }
}
/// <summary>
/// 生成验证码
/// </summary>
/// <param name="length">指定验证码的长度</param>
/// <returns></returns>
public string CreateValidateNumber(int length)
{
string[] CharArray = charcode.Split(',');//将字符串转换为字符数组
string randomCode = "";
int temp = -1;
Random rand = new Random();
for (int i = 0; i < length; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
}
int t = rand.Next(CharArray.Length - 1);
if (temp != -1 && temp == t)
{
return CreateValidateNumber(length);
}
temp = t;
randomCode += CharArray[t];
}
return randomCode;
}
/// <summary>
/// 创建验证码的图片
/// </summary>
/// <param name="context">context对象</param>
/// <param name="validateNum">验证码</param>
public void CreateValidateGraphic(HttpContext context,string validateNum)
{
int iwidth = (int)(validateNum.Length * 14);
Bitmap image = new Bitmap(iwidth, 22);
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的干扰线
for (int i = 0; i < 25; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),
Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(validateNum, font, brush, 3, 2);
//画图片的前景干扰点
for (int i = 0; i < 100; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
//保存图片数据
MemoryStream stream = new MemoryStream();
image.Save(stream, ImageFormat.Jpeg);
//输出图片
context.Current.Response.ClearContent();
context.Current.ContentType = "image/jpeg";
context.Current.BinaryWrite(stream.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
/// <summary>
/// 得到验证码图片的长度
/// </summary>
/// <param name="validateNumLength">验证码的长度</param>
/// <returns></returns>
public static int GetImageWidth(int validateNumLength)
{
return (int)(validateNumLength * 14);
}
/// <summary>
/// 得到验证码图片的高度
/// </summary>
/// <returns></returns>
public static double GetImageHeight()
{
return 22;
}
}
<TR height=30>
<TD>验证码:</TD>
<TD vAlign=center colSpan=2>
<asp:TextBox ID="txt_chkNum" autocomplete="off" height="23px" Width="100px" runat="server" ></asp:TextBox>
<a href="javascript:reloadcode();" title="更换一张验证码图片"><IMG src="checkNum.aspx?<%= checkNum %>" id="safecode" alt="看不清,请换一张" border=0 align="middle"></a> <a href="javascript:reloadcode();" title="更换一张">看不清</a> </TD>
</TR>
后台 if (this.txt_chkNum.Text != Session["CheckCode"].ToString())
{
CommonUse.alertMsg(this.Page, "验证码错误。");
this.txt_chkNum.Text = "";
this.txt_pwd.Focus();
return;
}
验证码生成页面 CheckNum.aspxusing System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
public partial class CheckNum : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// 在此处放置用户代码以初始化页面
string checkNum = CreateRandomCode();
Session["CheckCode"] = checkNum;
CreateImage(checkNum);
}
//产生随即验证码
private string CreateRandomCode()
{
//做个随机数库
string allChar = "0,1,2,3,4,5,6,7,8,9";
//将库里的字符放到数组里去
string[] allCharArray = allChar.Split(',');
//定义RandomCode变量,为了存放生成的随机数。
string RandomCode = "";
int temp = -1;
//实例Random函数
Random rand = new Random();
//for循环,这里是四次循环,为了是产生四个随机数,如果要产生多个,自己定义循环次数
for (int i = 0; i < 4; i++)
{
if (temp != -1)
{
//在非第一次循环后执行这里,根据DataTime产生一个随机种子,用来计算伪随机数序列起始值的数字
rand = new Random(i * temp * ((int)System.DateTime.Now.Ticks));
}
//返回一个小于指定最大值的非负随机数
int t = rand.Next(9);
if (temp == t)
{
return CreateRandomCode();
}
temp = t;
//将产生的值放入RandomCode变量中
RandomCode += allCharArray[t];
}
//返回产生的随机字符串
return RandomCode;
}
//这个是将数字产生图片的函数:
private void CreateImage(string RandomCode)
{
int iWidth;
//根据字符串的长度确定生成图片的宽度
iWidth = (int)(RandomCode.Length * 11.5);
//实例一个位图
System.Drawing.Bitmap image = new Bitmap(iWidth, 20);
//封装GDI+绘图面(此类无法继承)
System.Drawing.Graphics g = Graphics.FromImage(image);
//实例文本格式,包括字体,字号,字形
Font f = new Font("Arial", 10, System.Drawing.FontStyle.Bold);
//实例填充图形形状
System.Drawing.Brush b = new System.Drawing.SolidBrush(Color.Black);
//清楚整个绘图面并以指定背景色填充
g.Clear(Color.White);
//绘制吧
g.DrawString(RandomCode, f, b, 3, 3);
//创建一个内存流
System.IO.MemoryStream ms = new System.IO.MemoryStream();
//将此图像以指定的格式保存到指定的流中
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
//清楚缓存区流中的所有内容输出
Response.ClearContent();
//设置输出流的HTTP MIME类型
Response.ContentType = "image/Jpeg";
//将一个二进制字符串写入HTTP输出流
Response.BinaryWrite(ms.ToArray());
//释放资源
g.Dispose();
//释放资源
image.Dispose();
}
}