ValidateCode.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.IO;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Web;
  8. using Masuit.Tools.Systems;
  9. namespace Masuit.Tools.Strings
  10. {
  11. /// <summary>
  12. /// 画验证码
  13. /// </summary>
  14. public static class ValidateCode
  15. {
  16. /// <summary>
  17. /// 生成验证码
  18. /// </summary>
  19. /// <param name="length">指定验证码的长度</param>
  20. /// <returns>验证码字符串</returns>
  21. public static string CreateValidateCode(int length)
  22. {
  23. string ch = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ1234567890@#$%&?";
  24. byte[] b = new byte[4];
  25. using var cpt = new RNGCryptoServiceProvider();
  26. cpt.GetBytes(b);
  27. var r = new Random(BitConverter.ToInt32(b, 0));
  28. var sb = new StringBuilder();
  29. for (int i = 0; i < length; i++)
  30. {
  31. sb.Append(ch[r.Next(ch.Length)]);
  32. }
  33. return sb.ToString();
  34. }
  35. /// <summary>
  36. /// 创建验证码的图片
  37. /// </summary>
  38. /// <param name="validateCode">验证码序列</param>
  39. /// <param name="context">当前的HttpContext上下文对象</param>
  40. /// <param name="fontSize">字体大小,默认值22px</param>
  41. /// <param name="lineHeight">行高,默认36px</param>
  42. /// <exception cref="Exception">The operation failed.</exception>
  43. public static byte[] CreateValidateGraphic(this HttpContext context, string validateCode, int fontSize = 22, int lineHeight = 36)
  44. {
  45. using Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * (fontSize + 2.0)), lineHeight);
  46. using Graphics g = Graphics.FromImage(image);
  47. //生成随机生成器
  48. Random random = new Random();
  49. //清空图片背景色
  50. g.Clear(Color.White);
  51. //画图片的干扰线
  52. for (int i = 0; i < 75; i++)
  53. {
  54. int x1 = random.Next(image.Width);
  55. int x2 = random.Next(image.Width);
  56. int y1 = random.Next(image.Height);
  57. int y2 = random.Next(image.Height);
  58. g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
  59. }
  60. Font[] fonts =
  61. {
  62. new Font("Arial", fontSize, FontStyle.Bold | FontStyle.Italic),
  63. new Font("微软雅黑", fontSize, FontStyle.Bold | FontStyle.Italic),
  64. new Font("黑体", fontSize, FontStyle.Bold | FontStyle.Italic),
  65. new Font("宋体", fontSize, FontStyle.Bold | FontStyle.Italic),
  66. new Font("楷体", fontSize, FontStyle.Bold | FontStyle.Italic)
  67. };
  68. //渐变.
  69. using var brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
  70. g.DrawString(validateCode, fonts[new Random().Next(fonts.Length)], brush, 3, 2);
  71. //画图片的前景干扰点
  72. for (int i = 0; i < 300; i++)
  73. {
  74. int x = random.Next(image.Width);
  75. int y = random.Next(image.Height);
  76. image.SetPixel(x, y, Color.FromArgb(random.Next()));
  77. }
  78. //画图片的边框线
  79. g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
  80. //保存图片数据
  81. using var stream = new PooledMemoryStream();
  82. image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
  83. //输出图片流
  84. context.Response.Clear();
  85. context.Response.ContentType = "image/jpeg";
  86. return stream.ToArray();
  87. }
  88. }
  89. }