ValidateCode.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using Microsoft.AspNetCore.Http;
  2. using System;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Drawing.Imaging;
  6. using System.IO;
  7. using System.Security.Cryptography;
  8. using System.Text;
  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. new RNGCryptoServiceProvider().GetBytes(b);
  26. Random r = new Random(BitConverter.ToInt32(b, 0));
  27. StringBuilder 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. 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, ImageFormat.Jpeg);
  82. //输出图片流
  83. context.Response.Clear();
  84. context.Response.ContentType = "image/jpeg";
  85. return stream.ToArray();
  86. }
  87. }
  88. }