验证码

比眉伴天荒 2021-09-11 04:12 541阅读 0赞

js

换一组

<%—为a标签注册验证码事件—%>

一般处理程序制作验证码

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Web;
using System.Web.SessionState;//添加命名空间,用于保存session

namespace LiBangWuLiu
{
///


/// VerifyCode 的摘要说明
///

public class VerifyCode : IHttpHandler,IRequiresSessionState //实现IRequiresSessionState
{
///
/// 创建一副图片,画上验证码
///

///

  1. public void ProcessRequest(HttpContext context)
  2. \{
  3. context.Response.ContentType = "image/plain";//响应类型为图片image
  4. string validateCode = getValidateCode(4);//获取一个四位数字的验证码
  5. context.Session\["validateCode"\] = validateCode;//将验证码存到session中,
  6. //创建验证码图片
  7. using (Bitmap img = new Bitmap(60, 28))//创建具体位置图像,位图
  8. \{
  9. using (Graphics g = Graphics.FromImage(img))//从指定的img创建新的图面)
  10. \{
  11. g.Clear(Color.LightYellow);//初始化图片背景色
  12. g.DrawRectangle(new Pen(Color.Red), 0, 0, img.Width - 1, img.Height - 1);//画图片上矩形的边框,从坐标(0,0)开始,相对于图片的坐标
  13. Font font = new System.Drawing.Font("Arial", 16, (System.Drawing.FontStyle.Bold));//字体样式
  14. System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Blue, Color.DarkRed, 1.2F, true);//渐变效果
  15. g.DrawString(validateCode, font, brush, 2, 2);//将验证码写入图片
  16. //画前景线 ,随机生成几个点,(x1,y1)到(x2,y2)画线
  17. Random ran = new Random();
  18. int i;
  19. for (i = 0; i < 10; i++)
  20. \{
  21. int x1 = ran.Next(img.Width);
  22. int x2 = ran.Next(img.Width);
  23. int y1 = ran.Next(img.Height);
  24. int y2 = ran.Next(img.Height);
  25. g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
  26. \}
  27. \}
  28. img.Save(context.Response.OutputStream, ImageFormat.Jpeg);//将图片保存到响应流
  29. //string yanzhengma1 = context.Request.QueryString\["yzm"\];
  30. //context.Response.Write(yanzhengma(validateCode,yanzhengma1));//返回产生的随机数
  31. //context.Response.End();
  32. \}
  33. \}
  34. bool falg = false;
  35. public bool yanzhengma(string sjm,string yzm)
  36. \{
  37. if (sjm == yzm)
  38. \{
  39. falg = true;
  40. \}
  41. else \{
  42. falg = false;
  43. \}
  44. return falg;
  45. \}
  46. /// <summary>
  47. /// 生成随机码的方法,
  48. /// </summary>
  49. /// <param name="validateCodeLength">随机码的长度</param>
  50. /// <returns>随机码</returns>
  51. private string getValidateCode(int validateCodeLength)
  52. \{
  53. string allchar =@"123456789qwertyuioplkjhgfdsazxcvbnm123456789QWERTYUIOPLKJHGFDSAZXCVBNM";//列出所有可能出现的字符
  54. int length = allchar.Length;//获取所有字符的个数
  55. Random ran = new Random();//声明生成一个随机数的对象
  56. string validateCode = string.Empty;//声明随机码
  57. for (int i = 0; i < validateCodeLength; i++)
  58. \{
  59. validateCode += allchar\[ran.Next(length)\];//找到随机码,用生成的随机数作为字符串的索引,下标
  60. \}
  61. return validateCode;
  62. \}
  63. public bool IsReusable
  64. \{
  65. get
  66. \{
  67. return false;
  68. \}
  69. \}
  70. \}

}

发表评论

表情:
评论列表 (有 0 条评论,541人围观)

还没有评论,来说两句吧...

相关阅读