UU跑腿标准版

verify_code.ashx.cs 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.SessionState;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Drawing.Imaging;
  9. using HySoft.Common;
  10. namespace HySoft.BaseCallCenter.Web.tools
  11. {
  12. /// <summary>
  13. /// 验证码生成类
  14. /// </summary>
  15. public class verify_code : IHttpHandler, IRequiresSessionState
  16. {
  17. public void ProcessRequest(HttpContext context)
  18. {
  19. int codeW = 80;
  20. int codeH = 22;
  21. int fontSize = 16;
  22. string chkCode = string.Empty;
  23. //颜色列表,用于验证码、噪线、噪点
  24. Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
  25. //字体列表,用于验证码
  26. string[] font = { "Times New Roman", "Verdana", "Arial", "Gungsuh", "Impact" };
  27. //验证码的字符集,去掉了一些容易混淆的字符
  28. char[] character = { '2', '3', '4', '5', '6', '8', '9', 'a', 'b', 'd', 'e', 'f', 'h', 'k', 'm', 'n', 'r', 'x', 'y', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
  29. Random rnd = new Random();
  30. //生成验证码字符串
  31. for (int i = 0; i < 4; i++)
  32. {
  33. chkCode += character[rnd.Next(character.Length)];
  34. }
  35. //写入Session
  36. context.Session[DTKeys.SessionKey.VerifyCode] = chkCode.ToLower();
  37. //创建画布
  38. Bitmap bmp = new Bitmap(codeW, codeH);
  39. Graphics g = Graphics.FromImage(bmp);
  40. g.Clear(Color.White);
  41. //画噪线
  42. //for (int i = 0; i < 1; i++)
  43. //{
  44. // int x1 = rnd.Next(codeW);
  45. // int y1 = rnd.Next(codeH);
  46. // int x2 = rnd.Next(codeW);
  47. // int y2 = rnd.Next(codeH);
  48. // Color clr = color[rnd.Next(color.Length)];
  49. // g.DrawLine(new Pen(clr), x1, y1, x2, y2);
  50. //}
  51. //画验证码字符串
  52. for (int i = 0; i < chkCode.Length; i++)
  53. {
  54. string fnt = font[rnd.Next(font.Length)];
  55. Font ft = new Font(fnt, fontSize);
  56. Color clr = color[rnd.Next(color.Length)];
  57. g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 18 + 2, (float)0);
  58. }
  59. //画噪点
  60. //for (int i = 0; i < 100; i++)
  61. //{
  62. // int x = rnd.Next(bmp.Width);
  63. // int y = rnd.Next(bmp.Height);
  64. // Color clr = color[rnd.Next(color.Length)];
  65. // bmp.SetPixel(x, y, clr);
  66. //}
  67. //清除该页输出缓存,设置该页无缓存
  68. context.Response.Buffer = true;
  69. context.Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0);
  70. context.Response.Expires = 0;
  71. context.Response.CacheControl = "no-cache";
  72. context.Response.AppendHeader("Pragma", "No-Cache");
  73. //将验证码图片写入内存流,并将其以 "image/Png" 格式输出
  74. MemoryStream ms = new MemoryStream();
  75. try
  76. {
  77. bmp.Save(ms, ImageFormat.Png);
  78. context.Response.ClearContent();
  79. context.Response.ContentType = "image/Png";
  80. context.Response.BinaryWrite(ms.ToArray());
  81. }
  82. finally
  83. {
  84. //显式释放资源
  85. bmp.Dispose();
  86. g.Dispose();
  87. }
  88. }
  89. public bool IsReusable
  90. {
  91. get
  92. {
  93. return false;
  94. }
  95. }
  96. }
  97. }