ValidateCode.cs 3.9 KB

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