|
|
@@ -1,24 +1,18 @@
|
|
|
#if NET461
|
|
|
-
|
|
|
-using System;
|
|
|
-using System.Drawing;
|
|
|
-using System.Drawing.Drawing2D;
|
|
|
-using System.IO;
|
|
|
-using System.Security.Cryptography;
|
|
|
-using System.Text;
|
|
|
using System.Web;
|
|
|
-
|
|
|
#else
|
|
|
-
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
+#endif
|
|
|
+
|
|
|
using System;
|
|
|
using System.Drawing;
|
|
|
using System.Drawing.Drawing2D;
|
|
|
using System.IO;
|
|
|
using System.Security.Cryptography;
|
|
|
using System.Text;
|
|
|
+using System.Drawing.Imaging;
|
|
|
+using Masuit.Tools.AspNetCore.Mime;
|
|
|
|
|
|
-#endif
|
|
|
namespace Masuit.Tools.Strings
|
|
|
{
|
|
|
/// <summary>
|
|
|
@@ -41,7 +35,7 @@ namespace Masuit.Tools.Strings
|
|
|
var sb = new StringBuilder();
|
|
|
for (int i = 0; i < length; i++)
|
|
|
{
|
|
|
- sb.Append(ch[r.Next(ch.Length)]);
|
|
|
+ sb.Append(ch[r.StrictNext(ch.Length)]);
|
|
|
}
|
|
|
|
|
|
return sb.ToString();
|
|
|
@@ -57,7 +51,7 @@ namespace Masuit.Tools.Strings
|
|
|
/// <exception cref="Exception">The operation failed.</exception>
|
|
|
public static byte[] CreateValidateGraphic(this HttpContext context, string validateCode, int fontSize = 22, int lineHeight = 36)
|
|
|
{
|
|
|
- using Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * (fontSize + 2.0)), lineHeight);
|
|
|
+ using Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * (fontSize + 2.0)), lineHeight + 2);
|
|
|
using Graphics g = Graphics.FromImage(image);
|
|
|
//生成随机生成器
|
|
|
Random random = new Random();
|
|
|
@@ -66,11 +60,11 @@ namespace Masuit.Tools.Strings
|
|
|
//画图片的干扰线
|
|
|
for (int i = 0; i < 75; i++)
|
|
|
{
|
|
|
- int x1 = random.Next(image.Width);
|
|
|
- int x2 = random.Next(image.Width);
|
|
|
- int y1 = random.Next(image.Height);
|
|
|
- int y2 = random.Next(image.Height);
|
|
|
- g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
|
|
|
+ int x1 = random.StrictNext(image.Width);
|
|
|
+ int x2 = random.StrictNext(image.Width);
|
|
|
+ int y1 = random.StrictNext(image.Height);
|
|
|
+ int y2 = random.StrictNext(image.Height);
|
|
|
+ g.DrawLine(new Pen(Color.FromArgb(random.StrictNext(255), random.StrictNext(255), random.StrictNext(255))), x1, y1, x2, y2);
|
|
|
}
|
|
|
|
|
|
Font[] fonts =
|
|
|
@@ -83,24 +77,24 @@ namespace Masuit.Tools.Strings
|
|
|
};
|
|
|
//渐变.
|
|
|
using var brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
|
|
|
- g.DrawString(validateCode, fonts[new Random().Next(fonts.Length)], brush, 3, 2);
|
|
|
+ g.DrawString(validateCode, fonts[random.StrictNext(fonts.Length)], brush, 3, 2);
|
|
|
|
|
|
//画图片的前景干扰点
|
|
|
- for (int i = 0; i < 300; i++)
|
|
|
+ for (int i = 0; i < 350; i++)
|
|
|
{
|
|
|
- int x = random.Next(image.Width);
|
|
|
- int y = random.Next(image.Height);
|
|
|
- image.SetPixel(x, y, Color.FromArgb(random.Next()));
|
|
|
+ int x = random.StrictNext(image.Width);
|
|
|
+ int y = random.StrictNext(image.Height);
|
|
|
+ image.SetPixel(x, y, Color.FromArgb(random.StrictNext(255), random.StrictNext(255), random.StrictNext(255)));
|
|
|
}
|
|
|
|
|
|
//画图片的边框线
|
|
|
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
|
|
|
//保存图片数据
|
|
|
using MemoryStream stream = new MemoryStream();
|
|
|
- image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
|
|
|
+ image.Save(stream, ImageFormat.Jpeg);
|
|
|
//输出图片流
|
|
|
context.Response.Clear();
|
|
|
- context.Response.ContentType = "image/jpeg";
|
|
|
+ context.Response.ContentType = ContentType.Jpeg;
|
|
|
return stream.ToArray();
|
|
|
}
|
|
|
}
|