
📣读完这篇文章里你能收获到


我以Centos 7.x为例,其他系统自行百度:xx系统安装字体
如果没数据的话就继续往下走,先把字体库安装好
如果有数据的话,则跳过,直接进入第二点使用代码
fc-list
yum -y install fontconfig

用来搜索目录中所有的字体信息,并汇总生成fonts.scale文件
yum -y install ttmkfdir

fc-list

https://github.com/SixLabors/ImageSharp
using SixLabors.ImageSharp;
using SixLabors.Fonts;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Drawing.Processing;
using System;
using System.Linq;namespace VertifyCode
{public class VerifyCodeHelper{private static readonly Color[] Colors = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown,Color.Brown,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;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;}public static (string code, byte[] bytes) CreateValidateGraphic(int CodeLength, int Width = 80, int Height = 40, int FontSize = 14){var code = GenCode(CodeLength);var r = new Random();using var image = new Image(Width, Height);// 字体var font = SystemFonts.CreateFont(SystemFonts.Families.First().Name, FontSize, FontStyle.Bold);image.Mutate(ctx =>{// 白底背景ctx.Fill(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);return (code, ms.ToArray());}}
}
https://github.com/mono/SkiaSharp
using SkiaSharp;
using System;namespace VertifyCode
{public class VerifyCodeImageSharpHelper{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 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;}public static (string code, byte[] bytes) CreateValidateGraphic(int codeLength, int width = 80, int height = 40, int fontSize = 20){var code = GenCode(codeLength);byte[] imageBytes = null;int image2d_x = 0;int image2d_y = 0;SKRect size;int compensateDeepCharacters = 0;using (SKPaint drawStyle = CreatePaint(fontSize)){size = MeasureText(code, drawStyle);image2d_x = width;image2d_y = height;}using (SKBitmap image2d = new SKBitmap(image2d_x, image2d_y, SKColorType.Bgra8888, SKAlphaType.Premul)){using (SKCanvas canvas = new SKCanvas(image2d)){canvas.DrawColor(SKColors.Black); // Clearusing (SKPaint drawStyle = CreatePaint(fontSize)){canvas.DrawText(code, 0 + 5, image2d_y - 5 - compensateDeepCharacters, drawStyle);}using (SKImage img = SKImage.FromBitmap(image2d)){using (SKData p = img.Encode(SKEncodedImageFormat.Png, 100)){imageBytes = p.ToArray();}}}return (code,imageBytes);}}private static SKPaint CreatePaint(int fontSize){string font = @"";font = @"Arial";font = @"Liberation Serif";font = @"Segoe Script";font = @"Consolas";font = @"Comic Sans MS";font = @"SimSun";font = @"Impact";return CreatePaint(SKColors.White, font, fontSize, SKTypefaceStyle.Normal);}private static SKPaint CreatePaint(SKColor color, string fontName, float fontSize, SKTypefaceStyle fontStyle){SKTypeface font = SKTypeface.FromFamilyName(fontName, fontStyle);SKPaint paint = new SKPaint();paint.IsAntialias = true;paint.Color = color;// paint.StrokeCap = SKStrokeCap.Round;paint.Typeface = font;paint.TextSize = fontSize;return paint;}// MeasureText("Impact", 12, SKTypefaceStyle.Bold);internal static SKRect MeasureText(string text, SKPaint paint){SKRect rect = new SKRect();paint.MeasureText(text, ref rect);return rect;} // End Function MeasureText }
}