using SixLabors.ImageSharp
还需要用到 2 个的插件, SixLabors.Fonts 和 SixLabors.ImageSharp.Drawing
通过CLI手动安装
dotnet add package SixLabors.ImageSharp.Drawing --version 1.0.0-beta15
dotnet add package SixLabors.Fonts --version 1.0.0-beta19
using System.Text;
using NPOI.OpenXmlFormats.Spreadsheet;
using SixLabors.Fonts;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using ZXing;
namespace Cps.WebApi.Core.Wx
{
/// <summary>
/// 生成验证码
/// </summary>
public class ValidateCode
{
private static readonly SixLabors.ImageSharp.Color[] Colors = {
SixLabors.ImageSharp.Color.Black,
SixLabors.ImageSharp.Color.Red,
SixLabors.ImageSharp.Color.Blue,
SixLabors.ImageSharp.Color.Green,
SixLabors.ImageSharp.Color.Orange,
SixLabors.ImageSharp.Color.Brown,
SixLabors.ImageSharp.Color.Brown,
SixLabors.ImageSharp.Color.DarkBlue
};
private static readonly char[] Chars = { '2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
//private static readonly int Width = 90;
//private static readonly int Height = 35;
/// <summary>
/// 获取转码
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
private static string GenCode(int num)
{
var code = string.Empty;
var r = new Random();
for (int i = 0; i < num; i++)
{
code += Chars[r.Next(Chars.Length)].ToString();
}
return code;
}
/// <summary>
/// 生成验证码
/// </summary>
/// <param name="CodeLength"></param>
/// <param name="Width"></param>
/// <param name="Height"></param>
/// <param name="FontSize"></param>
/// <returns></returns>
public static (string code, string base64) CreateValidateGraphic(int CodeLength, int Width, int Height, int FontSize)
{
var code = GenCode(CodeLength);
var r = new Random();
using var image = new Image<Rgba32>(Width, Height);
// 字体
var font = SixLabors.Fonts.SystemFonts.CreateFont(SixLabors.Fonts.SystemFonts.Families.First().Name, FontSize, SixLabors.Fonts.FontStyle.Bold);
image.Mutate(ctx =>
{
// 白底背景
ctx.BackgroundColor(SixLabors.ImageSharp.Color.White);
// 画验证码
for (int i = 0; i < code.Length; i++)
{
ctx.DrawText(code[i].ToString()
, font
, Colors[r.Next(Colors.Length)]
, new PointF(20 * i + 10, r.Next(2, 12)));
}
// 画干扰线
for (int i = 0; i < 6; i++)
{
var pen = new Pen(Colors[r.Next(Colors.Length)], 1);
var p1 = new PointF(r.Next(Width), r.Next(Height));
var p2 = new PointF(r.Next(Width), r.Next(Height));
ctx.DrawLines(pen, p1, p2);
}
// 画噪点
for (int i = 0; i < 60; i++)
{
var pen = new Pen(Colors[r.Next(Colors.Length)], 1);
var p1 = new PointF(r.Next(Width), r.Next(Height));
var p2 = new PointF(p1.X + 1f, p1.Y + 1f);
ctx.DrawLines(pen, p1, p2);
}
});
using var ms = new System.IO.MemoryStream();
// 格式 自定义
image.SaveAsPng(ms);
//image.Save(ms, new PngEncoder());
byte[] imageBytes = ms.ToArray();
var base64String = Convert.ToBase64String(imageBytes);
return (code, "data:image/png;base64," + base64String);
}
}
}
调用方法
/// <summary>
/// 获取验证码
/// </summary>
/// <returns></returns>
[AllowAnonymous]
[HttpGet]
public IActionResult GetValidateGraphic()
{
try
{
var YzmData = ValidateCode.CreateValidateGraphic(4, 95, 40, 22);
System.Random random = new System.Random();
string RandomValue = random.Next(0, 2147483646).ToString();
CpsCache.SetDatas("LoginYzm_" + RandomValue, YzmData.code, (DateTime.Now.AddMinutes(5) - DateTime.Now));
var retData = new
{
RandomValue = RandomValue,
Img = YzmData.base64
};
return Success(retData);
}
catch (Exception ex)
{
return Fail("操作失败," + ex.Message);
}
}
标签:功能,验证码,Next,using,var,new,net,SixLabors,ImageSharp From: https://www.cnblogs.com/wtlcy/p/17596484.html