ValidateCode.cs 4.0 KB

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