|
@@ -1,7 +1,11 @@
|
|
|
-using System;
|
|
|
-using System.Drawing;
|
|
|
-using System.Drawing.Imaging;
|
|
|
+using SixLabors.Fonts;
|
|
|
+using SixLabors.ImageSharp;
|
|
|
+using SixLabors.ImageSharp.Drawing.Processing;
|
|
|
+using SixLabors.ImageSharp.PixelFormats;
|
|
|
+using SixLabors.ImageSharp.Processing;
|
|
|
+using System;
|
|
|
using System.IO;
|
|
|
+using System.Linq;
|
|
|
using System.Text;
|
|
|
using ZhonTai.Admin.Core.Attributes;
|
|
|
using ZhonTai.Admin.Core.Configs;
|
|
@@ -23,7 +27,7 @@ namespace ZhonTai.Admin.Tools.Captcha
|
|
|
var chars = new StringBuilder();
|
|
|
//验证码的字符集,去掉了一些容易混淆的字符
|
|
|
char[] character = { '2', '3', '4', '5', '6', '8', '9', 'a', 'b', 'd', 'e', 'f', 'h', 'k', 'm', 'n', 'r', 'x', 'y', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
|
|
|
- Random rnd = new Random();
|
|
|
+ var rnd = new Random();
|
|
|
//生成验证码字符串
|
|
|
for (int i = 0; i < length; i++)
|
|
|
{
|
|
@@ -39,19 +43,18 @@ namespace ZhonTai.Admin.Tools.Captcha
|
|
|
int fontSize = 22;
|
|
|
|
|
|
//颜色列表,用于验证码、噪线、噪点
|
|
|
- Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
|
|
|
+ Color[] colors = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
|
|
|
//字体列表,用于验证码
|
|
|
string[] fonts = _appConfig.VarifyCode.Fonts;
|
|
|
|
|
|
- code = GenerateRandom(length);
|
|
|
+ var chars = GenerateRandom(length);
|
|
|
+ code = chars;
|
|
|
|
|
|
- //创建画布
|
|
|
- using (Bitmap bmp = new Bitmap(codeW, codeH))
|
|
|
- using (Graphics g = Graphics.FromImage(bmp))
|
|
|
- using (MemoryStream ms = new MemoryStream())
|
|
|
+ using var img = new Image<Rgba32>(codeW, codeH, Color.White);
|
|
|
+
|
|
|
+ img.Mutate(ctx =>
|
|
|
{
|
|
|
- g.Clear(Color.White);
|
|
|
- Random rnd = new Random();
|
|
|
+ var rnd = new Random();
|
|
|
//画噪线
|
|
|
for (int i = 0; i < 1; i++)
|
|
|
{
|
|
@@ -59,28 +62,46 @@ namespace ZhonTai.Admin.Tools.Captcha
|
|
|
int y1 = rnd.Next(codeH);
|
|
|
int x2 = rnd.Next(codeW);
|
|
|
int y2 = rnd.Next(codeH);
|
|
|
- Color clr = color[rnd.Next(color.Length)];
|
|
|
- g.DrawLine(new Pen(clr), x1, y1, x2, y2);
|
|
|
+ int ctrlx1 = rnd.Next(codeW / 4, codeW / 4 * 3), ctrly1 = rnd.Next(5, codeH - 5);
|
|
|
+ int ctrlx2 = rnd.Next(codeW / 4, codeW / 4 * 3), ctrly2 = rnd.Next(5, codeH - 5);
|
|
|
+ Color color = colors[rnd.Next(colors.Length)];
|
|
|
+ ctx.DrawBeziers(new DrawingOptions
|
|
|
+ {
|
|
|
+ GraphicsOptions = new GraphicsOptions
|
|
|
+ {
|
|
|
+ BlendPercentage = 1
|
|
|
+ }
|
|
|
+ }, color, 1, new PointF(x1, y1), new PointF(ctrlx1, ctrly1), new PointF(ctrlx2, ctrly2), new PointF(x2, y2));
|
|
|
}
|
|
|
|
|
|
//画验证码字符串
|
|
|
{
|
|
|
- string fnt;
|
|
|
- Font ft;
|
|
|
- Color clr;
|
|
|
- for (int i = 0; i < code.Length; i++)
|
|
|
+ Color color;
|
|
|
+ Font font;
|
|
|
+ string fontName;
|
|
|
+ FontFamily fontFamily;
|
|
|
+ for (int i = 0; i < length; i++)
|
|
|
{
|
|
|
- fnt = fonts[rnd.Next(fonts.Length)];
|
|
|
- ft = new Font(fnt, fontSize);
|
|
|
- clr = color[rnd.Next(color.Length)];
|
|
|
- g.DrawString(code[i].ToString(), ft, new SolidBrush(clr), (float)i * 24 + 2, (float)0);
|
|
|
+ fontName = fonts[rnd.Next(fonts.Length)];
|
|
|
+ fontFamily = SystemFonts.Families.Where(a => a.Name == fontName).FirstOrDefault();
|
|
|
+ fontFamily = fontFamily.Name.NotNull() ? fontFamily : SystemFonts.Families.FirstOrDefault();
|
|
|
+ font = new Font(fontFamily, fontSize);
|
|
|
+ color = colors[rnd.Next(colors.Length)];
|
|
|
+ ctx.DrawText(new DrawingOptions
|
|
|
+ {
|
|
|
+ GraphicsOptions = new GraphicsOptions
|
|
|
+ {
|
|
|
+ BlendPercentage = 1
|
|
|
+ }
|
|
|
+ }, chars[i].ToString(), font, color, new PointF((float)i * 24 + 2, 0));
|
|
|
}
|
|
|
}
|
|
|
+ });
|
|
|
|
|
|
- //将验证码图片写入内存流,并将其以 "image/Png" 格式输出
|
|
|
- bmp.Save(ms, ImageFormat.Png);
|
|
|
- return ms.ToArray();
|
|
|
- }
|
|
|
+ //将验证码图片写入内存流,并将其以 "image/Png" 格式输出
|
|
|
+ using var ms = new MemoryStream();
|
|
|
+ img.SaveAsPng(ms);
|
|
|
+ return ms.ToArray();
|
|
|
}
|
|
|
|
|
|
public string GetBase64String(out string code, int length = 4)
|