.netCore 图形验证码,非System.Drawing.Common
netcore需要跨平台,说白点就是放在windows服务器要能用,放在linux服务器上也能用,甚至macos上。
很多时候需要使用到图形验证码,这就有问题了。
旧方案
1.引入包
<PackageReference Include="System.Drawing.Common" Version="5.0.3" />
2.添加引用
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
3.在linux上安装libgdiplus
问题在于这个libgdiplus东西非常大,这个东西是moon兼容而来的,而且!!!.net6.0开始不支持这个东西了。
新方案
1.安装包
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta15" />
要勾选预览版,不然找不到这个包
2.添加引用
using SixLabors.Fonts;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
3.生成一个验证码图片
public byte[] CreateByteByImgVerifyCode(string verifyCode, int width, int height)
{ using Image image = new Image<Rgba32>(width, height);
//漆底色白色
image.Mutate(x => x.DrawLines(Pens.DashDot(Color.White, width), new PointF[] { new PointF() { X = 0, Y = 0 }, new PointF() { X = width, Y = height } })); FontCollection collection = new();
FontFamily family = collection.Add("font/font.ttf");
Font font = family.CreateFont(20, FontStyle.Bold); PointF startPointF = new PointF(5, 5);
Random random = new Random(); //随机数产生器 Color[] colors = new Color[] { Color.Red, Color.Blue, Color.Green, Color.Purple, Color.Peru, Color.LightSeaGreen, Color.Lime, Color.Magenta, Color.Maroon, Color.MediumBlue, Color.MidnightBlue, Color.Navy };
//绘制大小
for (int i = 0; i < verifyCode.Length; i++)
{
image.Mutate(x => x.DrawText(verifyCode[i].ToString(), font, colors[random.Next(colors.Length)], startPointF));
//Console.WriteLine($"draw code:{verifyCode[i]} point:{startPointF.X}-{startPointF.Y}");
startPointF.X += (int)(width - 10) / verifyCode.Length;
startPointF.Y = random.Next(5, 10);
} IPen pen = Pens.DashDot(Color.Silver, 1); //绘制干扰线
for (var k = 0; k < 40; k++)
{
PointF[] points = new PointF[2];
points[0] = new PointF(random.Next(width), random.Next(height));
points[1] = new PointF(random.Next(width), random.Next(height));
image.Mutate(x => x.DrawLines(pen, points));
} using MemoryStream stream = new MemoryStream();
image.Save(stream, JpegFormat.Instance);
//输出图片流
return stream.ToArray(); }
4.在controller中调用它
[HttpGet]
public FileContentResult Code(string guid)
{
try
{
if (String.IsNullOrEmpty(guid))
{
throw new Exception("验证码代码错误,guid不能为空!");
} //进行特殊符号的替换工作
if (!new System.Text.RegularExpressions.Regex("[0-9,a-z,A-Z]{16}").Match(guid).Success)
{
throw new Exception("guid的位数不足,应为16位随机数,不能包含特殊符号,需要为字母和数字的组合");
} if (_cache.KeyExits(string.Format(PublicString.CacheImageHead, guid))) { throw new Exception("guid不能重复使用!"); } //判断guid是否存在 string code = _imgHelper.CreateVerifyCode(ImageHelper.VerifyCodeType.NumberVerifyCode); _cache.SetString(string.Format(PublicString.CacheImageHead, guid), code, 300); byte[] codeImage = _imgHelper.CreateByteByImgVerifyCode(code, 80, 36); return File(codeImage, @"image/jpeg");
}
catch (Exception exl)
{
_logger.LogException(exl);
throw new Exception(exl.Message);
}
}
5.随机数计算,缓存帮助类自己实现。
6.新方案不需要安装libgdiplus
7.旧方案占用内存很大,新方案内存消耗很划算
附上一个Dockerfile的文件内容
FROM mcr.microsoft.com/dotnet/aspnet:6.0-alpine AS base
# 安装tzdata
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
RUN apk add --no-cache tzdata
#RUN apk add libgdiplus --update-cache --repository http://dl-3.alpinelinux.org/alpine/edge/testing/ --allow-untrusted
#RUN apk add terminus-font
# 设置时区
ENV TZ="Asia/Shanghai"
ENV LANG C.UTF-8 FROM base AS final
WORKDIR /app
EXPOSE 80
COPY . . ENTRYPOINT ["dotnet", "xxx.HttpApi.Host.dll"]
当前为草稿,暂未完善
.netCore 图形验证码,非System.Drawing.Common的更多相关文章
- Linux/Docker 中使用 System.Drawing.Common 踩坑小计
前言 在项目迁移到 .net core 上面后,我们可以使用 System.Drawing.Common 组件来操作 Image,Bitmap 类型,实现生成验证码.二维码,图片操作等功能.Syste ...
- Asp.Net Core使用System.Drawing.Common部署到docker报错问题
Asp.Net Core 2.1发布后,正式支持System.Drawing.Common绘图了,可以用来做一些图片验证码之类的功能.但是把网站部署到docker容器里运行会遇到很多问题,也是非常闹心 ...
- .Net Core 使用 System.Drawing.Common 部署到CentOS上遇到的问题
一开始报这个错误:Unable to load shared library 'libdl' 找到libdl安装位置是/usr/lib64: #locate libdl /usr/lib64/libd ...
- Install-Package:QRCoder已拥有为System.Drawing.Common定义的依赖项
error_log PM> Install-Package QRCoder -Version 1.3.3 Install-Package : "QRCoder"已拥有为&qu ...
- .NET Core System.Drawing.Common 中文乱码的坑
最近在写一个汉字取点阵的程序,最开始是在win环境下运行的,没发现什么异常,然后今天把程序放在centos 下后发现英文正常,中文完全变成两位的字了,最开始是字体的原因 在把宋体等安装到centos ...
- .Net Core 使用 System.Drawing.Common 在CentOS下报错
.Net Core控制台项目,添加了 System.Drawing.Common 引用 #locate libdl /usr/lib64/libdl-2.17.so /usr/lib64/libdl. ...
- 在linux 或docker中使用 system.drawing.common
在dockerfile 中添加 FROM microsoft/dotnet:2.1-aspnetcore-runtime RUN apt-get update RUN apt-get install ...
- netcore发布到centos 验证码Zkweb.system.drawing不显示及乱码的问题
netcore发布到centos 使用的是Zkweb.system.drawing生成验证码,发布后可能会出现不显示及乱码的情况 1.验证码图片不显示(通过日志会发现生成图片时代码已经异常) Zkwe ...
- asp.net core 2.1 容器中使用 System.Drawing.Common 的问题
- .netcore中无法使用System.Drawing --解决方案
问题重现: 无法正常使用 解决方法: 安装System.Drawing.Common的NuGet就能正常使用了 操作之后: 这个是.netcoe中的解决办法,.net framework解决方案中添 ...
随机推荐
- AI 和 DevOps:实现高效软件交付的完美组合
AI 时代,DevOps 与 AI 共价结合.AI 由业务需求驱动,提高软件质量,而 DevOps 则从整体提升系统功能.DevOps 团队可以使用 AI 来进行测试.开发.监控.增强和系统发布.AI ...
- 自己动手实现rpc框架(二) 实现集群间rpc通信
自己动手实现rpc框架(二) 实现集群间rpc通信 1. 集群间rpc通信 上一篇博客中MyRpc框架实现了基本的点对点rpc通信功能.而在这篇博客中我们需要实现MyRpc的集群间rpc通信功能. 自 ...
- 微信小程序 - WXML 模板语法
[黑马程序员前端微信小程序开发教程,微信小程序从基础到发布全流程_企业级商城实战(含uni-app项目多端部署)] https://www.bilibili.com/video/BV1834y1676 ...
- Button按钮:得到鼠标焦点后自动放大,失去鼠标焦点后自动缩小_
作用 程序设计过程中,我们经常需要增加一些动态效果,以此改善用户的使用体验.本文将介绍一种方法,动态显示按钮状态,使其得到鼠标焦点后自动放大,失去鼠标焦点后自动缩小. 效果图 先放一张原图(鼠标还未移 ...
- DLL注入-Windows消息钩取
0x01 钩子 钩子,英文Hook,泛指钓取所需东西而使用的一切工具.后来延伸为"偷看或截取信息时所用的手段或工具". 挂钩:为了偷看或截取来往信息而在中间设置岗哨的行为 钩取:偷 ...
- String s=new String(“hello”)的执行过程
一. 介绍 String 是Java.long包下的String类,是一个特殊的引用类型,用于表示字符串.它提供了许多方法来操作和处理字符串,比如连接.截取.查找.替换等.String类内部使用字符数 ...
- k8s+containerd安装
准备环境 准备两台服务器节点,如果需要安装虚拟机,可以参考<wmware和centos安装过程> 机器名 IP 角色 CPU 内存 centos01 192.168.109.130 mas ...
- .NET 如何实现ChatGPT的Stream传输
.NET 如何实现ChatGPT的Stream传输 ChatGPT是如何实现不适用websocket进行一个一个字返回到前端的? 下面我们会介绍一下EventSource EventSource Ev ...
- Django: Token分发
Django后台token分发 在settings.py中引入 INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'd ...
- 八万乌云漏洞库——Ubuntu本地Docker搭建环境
环境准备 ubuntu16.04虚拟机 换国内源 vi /etc/apt/sources.list 阿里源 deb http://mirrors.aliyun.com/ubuntu/ xenial m ...