ValidateCode.cs 3.7 KB

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