ValidateCode.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. {
  46. using (Graphics g = Graphics.FromImage(image))
  47. {
  48. //生成随机生成器
  49. Random random = new Random();
  50. //清空图片背景色
  51. g.Clear(Color.White);
  52. //画图片的干扰线
  53. for (int i = 0; i < 75; i++)
  54. {
  55. int x1 = random.Next(image.Width);
  56. int x2 = random.Next(image.Width);
  57. int y1 = random.Next(image.Height);
  58. int y2 = random.Next(image.Height);
  59. g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
  60. }
  61. Font[] fonts =
  62. {
  63. new Font("Arial", 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. new Font("楷体", fontSize, FontStyle.Bold | FontStyle.Italic)
  68. };
  69. //渐变.
  70. LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
  71. g.DrawString(validateCode, fonts[new Random().Next(fonts.Length)], brush, 3, 2);
  72. //画图片的前景干扰点
  73. for (int i = 0; i < 300; i++)
  74. {
  75. int x = random.Next(image.Width);
  76. int y = random.Next(image.Height);
  77. image.SetPixel(x, y, Color.FromArgb(random.Next()));
  78. }
  79. //画图片的边框线
  80. g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
  81. //保存图片数据
  82. using (MemoryStream stream = new MemoryStream())
  83. {
  84. image.Save(stream, ImageFormat.Jpeg);
  85. //输出图片流
  86. context.Response.Clear();
  87. context.Response.ContentType = "image/jpeg";
  88. return stream.ToArray();
  89. }
  90. }
  91. }
  92. }
  93. }
  94. }