- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.Drawing;
- /// <summary>
- /// CheckCode 的摘要说明
- /// </summary>
- public class CheckCode
- {
- private HttpContext m_CurrentHttpContext;
- public CheckCode()
- {
- //
- // TODO: 在此处添加构造函数逻辑
- //
- }
- /// <summary>
- /// 初始化当前验证码类
- /// </summary>
- /// <param name="context"></param>
- public CheckCode(HttpContext context)
- {
- this.CurrentHttpContext = context;
- }
- /// <summary>
- /// 获取或者设置当前Http处理上下文
- /// </summary>
- public HttpContext CurrentHttpContext
- {
- get
- {
- return m_CurrentHttpContext;
- }
- set
- {
- m_CurrentHttpContext = value;
- }
- }
- /// <summary>
- /// 比较当前输入的验证码字符是否跟当前用户在页面使用的验证吗内容一致
- /// </summary>
- /// <param name="inputCode">输入的验证码</param>
- /// <returns></returns>
- public bool CompareCheckCode(string inputCode)
- {
- if (CurrentHttpContext.Session["CheckCode_codes"] == null || CurrentHttpContext.Session["CheckCode_codes"].ToString() == "")
- return false;
- return inputCode == CurrentHttpContext.Session["CheckCode_codes"].ToString();
- }
- /// <summary>
- /// 计算出5位随机数
- /// </summary>
- /// <returns></returns>
- public string GenerateCheckCode()
- {
- int number;
- char code;
- string checkCode = String.Empty;
- System.Random random = new Random();
- for (int i = 0; i < 5; i++)
- {
- number = random.Next();
- if (number % 2 == 0)
- code = Convert.ToChar('0' + (char)(number % 10));
- else
- code = Convert.ToChar('A' + (char)(number % 26));
- checkCode = checkCode + code.ToString();
- }
- CurrentHttpContext.Session.Add("CheckCode_codes", checkCode);
- return checkCode;
- }
- /// <summary>
- /// 生成验证码图片
- /// </summary>
- /// <param name="checkCode"></param>
- public void GenerateCheckCodeImage(string checkCode)
- {
- if (checkCode == null || checkCode.Trim() == String.Empty)
- return;
- //创建60×25的图片
- System.Drawing.Bitmap image = new System.Drawing.Bitmap(60, 25);
- 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, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
- System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
- g.DrawString(checkCode, font, brush, 2, 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);
- System.IO.MemoryStream ms = new System.IO.MemoryStream();
- image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
- CurrentHttpContext.Response.ClearContent();
- CurrentHttpContext.Response.ContentType = "image/gif";
- CurrentHttpContext.Response.BinaryWrite(ms.ToArray());
- }
- finally
- {
- g.Dispose();
- image.Dispose();
- }
- }
- }
调用方法:
protected void Page_Load(object sender, EventArgs e)
{
CheckCode cc = new CheckCode();
cc.CurrentHttpContext = this.Context;
string strCode=cc.GenerateCheckCode();
cc.GenerateCheckCodeImage(strCode);
}