一个生成验证码的程序

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.Drawing;
  11. /// <summary>
  12. /// CheckCode 的摘要说明
  13. /// </summary>
  14. public class CheckCode
  15. {
  16.     private HttpContext m_CurrentHttpContext;
  17.     public CheckCode()
  18.     {
  19.         //
  20.         // TODO: 在此处添加构造函数逻辑
  21.         //
  22.     }
  23.     /// <summary>
  24.     /// 初始化当前验证码类
  25.     /// </summary>
  26.     /// <param name="context"></param>
  27.     public CheckCode(HttpContext context)
  28.     {
  29.         this.CurrentHttpContext = context;
  30.     }
  31.     /// <summary>
  32.     /// 获取或者设置当前Http处理上下文
  33.     /// </summary>
  34.     public HttpContext CurrentHttpContext
  35.     {
  36.         get
  37.         {
  38.             return m_CurrentHttpContext;
  39.         }
  40.         set
  41.         {
  42.             m_CurrentHttpContext = value;
  43.         }
  44.     }
  45.     /// <summary>
  46.     /// 比较当前输入的验证码字符是否跟当前用户在页面使用的验证吗内容一致
  47.     /// </summary>
  48.     /// <param name="inputCode">输入的验证码</param>
  49.     /// <returns></returns>
  50.     public bool CompareCheckCode(string inputCode)
  51.     {
  52.         if (CurrentHttpContext.Session["CheckCode_codes"] == null || CurrentHttpContext.Session["CheckCode_codes"].ToString() == "")
  53.             return false;
  54.         return inputCode == CurrentHttpContext.Session["CheckCode_codes"].ToString();
  55.     }
  56.     /// <summary>
  57.     /// 计算出5位随机数
  58.     /// </summary>
  59.     /// <returns></returns>
  60.     public  string GenerateCheckCode()
  61.     {
  62.         int number;
  63.         char code;
  64.         string checkCode = String.Empty;
  65.         System.Random random = new Random();
  66.         for (int i = 0; i < 5; i++)
  67.         {
  68.             number = random.Next();
  69.             if (number % 2 == 0)
  70.                 code = Convert.ToChar('0' + (char)(number % 10));
  71.             else
  72.                 code = Convert.ToChar('A' + (char)(number % 26));
  73.             checkCode = checkCode + code.ToString();
  74.         }
  75.         CurrentHttpContext.Session.Add("CheckCode_codes", checkCode);
  76.         return checkCode;
  77.     }
  78.     /// <summary>
  79.     /// 生成验证码图片
  80.     /// </summary>
  81.     /// <param name="checkCode"></param>
  82.     public  void GenerateCheckCodeImage(string checkCode)
  83.     {
  84.         if (checkCode == null || checkCode.Trim() == String.Empty)
  85.             return;
  86.         //创建60×25的图片
  87.         System.Drawing.Bitmap image = new System.Drawing.Bitmap(60, 25);
  88.         Graphics g = Graphics.FromImage(image);
  89.         try
  90.         {
  91.             Random random = new Random();
  92.             g.Clear(Color.White);
  93.             //画干扰线
  94.             for (int i = 0; i < 25; i++)
  95.             {
  96.                 int x1 = random.Next(image.Width);
  97.                 int x2 = random.Next(image.Width);
  98.                 int y1 = random.Next(image.Height);
  99.                 int y2 = random.Next(image.Height);
  100.                 g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
  101.             }
  102.             Font font = new Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
  103.             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);
  104.             g.DrawString(checkCode, font, brush, 2, 2);
  105.             //画图片的前景噪音点
  106.             for (int i = 0; i < 100; i++)
  107.             {
  108.                 int x = random.Next(image.Width);
  109.                 int y = random.Next(image.Height);
  110.                 image.SetPixel(x, y, Color.FromArgb(random.Next()));
  111.             }
  112.             g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
  113.             System.IO.MemoryStream ms = new System.IO.MemoryStream();
  114.             image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
  115.             CurrentHttpContext.Response.ClearContent();
  116.             CurrentHttpContext.Response.ContentType = "image/gif";
  117.             CurrentHttpContext.Response.BinaryWrite(ms.ToArray());
  118.         }
  119.         finally
  120.         {
  121.             g.Dispose();
  122.             image.Dispose();
  123.         }
  124.     }
  125. }

 

调用方法:

 protected void Page_Load(object sender, EventArgs e)
    {
        CheckCode cc = new CheckCode();
        cc.CurrentHttpContext = this.Context;
        string strCode=cc.GenerateCheckCode();
        cc.GenerateCheckCodeImage(strCode);
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值