ValidateCode.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #if NET461
  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. using System.Web;
  10. namespace Masuit.Tools.Strings
  11. {
  12. /// <summary>
  13. /// 画验证码
  14. /// </summary>
  15. public static class ValidateCode
  16. {
  17. /// <summary>
  18. /// 生成验证码
  19. /// </summary>
  20. /// <param name="length">指定验证码的长度</param>
  21. /// <returns>验证码字符串</returns>
  22. public static string CreateValidateCode(int length)
  23. {
  24. string ch = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ1234567890@#$%&?";
  25. byte[] b = new byte[4];
  26. using var cpt = new RNGCryptoServiceProvider();
  27. cpt.GetBytes(b);
  28. var r = new Random(BitConverter.ToInt32(b, 0));
  29. var sb = new StringBuilder();
  30. for (int i = 0 ; i < length ; i++)
  31. {
  32. sb.Append(ch[r.Next(ch.Length)]);
  33. }
  34. return sb.ToString();
  35. }
  36. /// <summary>
  37. /// 创建验证码的图片
  38. /// </summary>
  39. /// <param name="validateCode">验证码序列</param>
  40. /// <param name="context">当前的HttpContext上下文对象</param>
  41. /// <param name="fontSize">字体大小,默认值22px</param>
  42. /// <param name="lineHeight">行高,默认36px</param>
  43. /// <exception cref="Exception">The operation failed.</exception>
  44. public static byte[] CreateValidateGraphic(this HttpContext context, string validateCode, int fontSize = 22, int lineHeight = 36)
  45. {
  46. using Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * (fontSize + 2.0)), lineHeight);
  47. using Graphics g = Graphics.FromImage(image);
  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. using var 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. image.Save(stream, ImageFormat.Jpeg);
  84. //输出图片流
  85. context.Response.Clear();
  86. context.Response.ContentType = "image/jpeg";
  87. return stream.ToArray();
  88. }
  89. }
  90. }
  91. #else
  92. using System;
  93. using System.Drawing;
  94. using System.IO;
  95. using System.Security.Cryptography;
  96. using System.Text;
  97. using Microsoft.AspNetCore.Http;
  98. using System.Drawing.Drawing2D;
  99. namespace Masuit.Tools.Strings
  100. {
  101. /// <summary>
  102. /// 画验证码
  103. /// </summary>
  104. public static class ValidateCode
  105. {
  106. /// <summary>
  107. /// 生成验证码
  108. /// </summary>
  109. /// <param name="length">指定验证码的长度</param>
  110. /// <returns>验证码字符串</returns>
  111. public static string CreateValidateCode(int length)
  112. {
  113. string ch = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ1234567890@#$%&?";
  114. byte[] b = new byte[4];
  115. using var cpt = new RNGCryptoServiceProvider();
  116. cpt.GetBytes(b);
  117. var r = new Random(BitConverter.ToInt32(b, 0));
  118. var sb = new StringBuilder();
  119. for (int i = 0 ; i < length ; i++)
  120. {
  121. sb.Append(ch[r.Next(ch.Length)]);
  122. }
  123. return sb.ToString();
  124. }
  125. /// <summary>
  126. /// 创建验证码的图片
  127. /// </summary>
  128. /// <param name="validateCode">验证码序列</param>
  129. /// <param name="context">当前的HttpContext上下文对象</param>
  130. /// <param name="fontSize">字体大小,默认值22px</param>
  131. /// <param name="lineHeight">行高,默认36px</param>
  132. /// <exception cref="Exception">The operation failed.</exception>
  133. public static byte[] CreateValidateGraphic(this HttpContext context, string validateCode, int fontSize = 22, int lineHeight = 36)
  134. {
  135. using Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * (fontSize + 2.0)), lineHeight);
  136. using Graphics g = Graphics.FromImage(image);
  137. //生成随机生成器
  138. Random random = new Random();
  139. //清空图片背景色
  140. g.Clear(Color.White);
  141. //画图片的干扰线
  142. for (int i = 0 ; i < 75 ; i++)
  143. {
  144. int x1 = random.Next(image.Width);
  145. int x2 = random.Next(image.Width);
  146. int y1 = random.Next(image.Height);
  147. int y2 = random.Next(image.Height);
  148. g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
  149. }
  150. Font[] fonts =
  151. {
  152. new Font("Arial", fontSize, FontStyle.Bold | FontStyle.Italic),
  153. new Font("微软雅黑", fontSize, FontStyle.Bold | FontStyle.Italic),
  154. new Font("黑体", fontSize, FontStyle.Bold | FontStyle.Italic),
  155. new Font("宋体", fontSize, FontStyle.Bold | FontStyle.Italic),
  156. new Font("楷体", fontSize, FontStyle.Bold | FontStyle.Italic)
  157. };
  158. //渐变.
  159. using var brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
  160. g.DrawString(validateCode, fonts[new Random().Next(fonts.Length)], brush, 3, 2);
  161. //画图片的前景干扰点
  162. for (int i = 0 ; i < 300 ; i++)
  163. {
  164. int x = random.Next(image.Width);
  165. int y = random.Next(image.Height);
  166. image.SetPixel(x, y, Color.FromArgb(random.Next()));
  167. }
  168. //画图片的边框线
  169. g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
  170. //保存图片数据
  171. using MemoryStream stream = new MemoryStream();
  172. image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
  173. //输出图片流
  174. context.Response.Clear();
  175. context.Response.ContentType = "image/jpeg";
  176. return stream.ToArray();
  177. }
  178. }
  179. }
  180. #endif