先设置一个登陆界面:
<div class="reb">
验证码:
<input id="txtU_Check" type="text" runat="server" style=" 85px;" maxlength="5" />
<img id="ValidateImg" align="absmiddle" title="看不清?点击更换" alt="看不清?点击更换" src="ValidateImg.aspx"
style="cursor: pointer; vertical-align: middle;" onclick="this.src=this.src+'?'" />
</div>
设置一个空白页面:VateImage.aspx,在这个页面用来调用验证码生成的类的方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class VateImage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ValidateCode.CreateCheckCodeImage();
}
}
建立画验证码类:ValidateCode 代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing.Imaging;
/// <summary>
///ValidateCode 的摘要说明
///生成验证校验
/// </summary>
public class ValidateCode
{ public ValidateCode()
{
// //TODO: 在此处添加构造函数逻辑 //
}
/// <summary> /// 创建验证码图片 ///
</summary>
public static void CreateCheckCodeImage()
{
CreateCheckCodeImage(GenerateCheckCode());
}
/// <summary>
/// 创建验证码图片
/// </summary>
/// <param name="checkCode"></param>
private static void CreateCheckCodeImage(string checkCode)
{ if ((checkCode != null) && !(checkCode.Trim() == string.Empty))
{ Bitmap image = new Bitmap((int)Math.Ceiling((double)(checkCode.Length * 12.5)), 0x16);
Graphics graphics = Graphics.FromImage(image);
try
{
int num;
Random random = new Random();
graphics.Clear(Color.White);
for (num = 0; num < 0x19; num++)
{
int num2 = random.Next(image.Width);
int num3 = random.Next(image.Width);
int num4 = random.Next(image.Height);
int num5 = random.Next(image.Height);
graphics.DrawLine(new Pen(Color.Silver), num2, num4, num3, num5);
}
Font font = new Font("Arial", 12f, FontStyle.Italic | FontStyle.Bold);
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
graphics.DrawString(checkCode, font, brush, (float)2f, (float)2f);
for (num = 0; num < 100; num++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
graphics.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
MemoryStream stream = new MemoryStream();
image.Save(stream, ImageFormat.Gif);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = "image/Gif";
HttpContext.Current.Response.BinaryWrite(stream.ToArray());
}
catch (Exception exception)
{
HttpContext.Current.Response.Redirect("ErrorMessage.aspx@Error=" + exception);
}
finally
{
graphics.Dispose();
image.Dispose();
}
}
}
/// <summary>
/// 生成验证码
/// </summary>
/// <returns></returns>
private static string GenerateCheckCode()
{
string str = getRodomNum();
HttpContext.Current.Session.Add("CheckCode", str);
return str;
}
/// <summary>
/// 英文字符
/// </summary>
/// <returns></returns>
private static string GetEngLish()
{
string[] strArray = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "M", "L", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
return strArray[GetMes.Random(0, 0x19)];
}
private static string getRodomNum()
{
return GetMes.Random(0x3e8, 0x270f).ToString();
}
}