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的更多相关文章

  1. Linux/Docker 中使用 System.Drawing.Common 踩坑小计

    前言 在项目迁移到 .net core 上面后,我们可以使用 System.Drawing.Common 组件来操作 Image,Bitmap 类型,实现生成验证码.二维码,图片操作等功能.Syste ...

  2. Asp.Net Core使用System.Drawing.Common部署到docker报错问题

    Asp.Net Core 2.1发布后,正式支持System.Drawing.Common绘图了,可以用来做一些图片验证码之类的功能.但是把网站部署到docker容器里运行会遇到很多问题,也是非常闹心 ...

  3. .Net Core 使用 System.Drawing.Common 部署到CentOS上遇到的问题

    一开始报这个错误:Unable to load shared library 'libdl' 找到libdl安装位置是/usr/lib64: #locate libdl /usr/lib64/libd ...

  4. Install-Package:QRCoder已拥有为System.Drawing.Common定义的依赖项

    error_log PM> Install-Package QRCoder -Version 1.3.3 Install-Package : "QRCoder"已拥有为&qu ...

  5. .NET Core System.Drawing.Common 中文乱码的坑

    最近在写一个汉字取点阵的程序,最开始是在win环境下运行的,没发现什么异常,然后今天把程序放在centos 下后发现英文正常,中文完全变成两位的字了,最开始是字体的原因 在把宋体等安装到centos ...

  6. .Net Core 使用 System.Drawing.Common 在CentOS下报错

    .Net Core控制台项目,添加了 System.Drawing.Common 引用 #locate libdl /usr/lib64/libdl-2.17.so /usr/lib64/libdl. ...

  7. 在linux 或docker中使用 system.drawing.common

    在dockerfile 中添加 FROM microsoft/dotnet:2.1-aspnetcore-runtime RUN apt-get update RUN apt-get install ...

  8. netcore发布到centos 验证码Zkweb.system.drawing不显示及乱码的问题

    netcore发布到centos 使用的是Zkweb.system.drawing生成验证码,发布后可能会出现不显示及乱码的情况 1.验证码图片不显示(通过日志会发现生成图片时代码已经异常) Zkwe ...

  9. asp.net core 2.1 容器中使用 System.Drawing.Common 的问题

  10. .netcore中无法使用System.Drawing --解决方案

    问题重现: 无法正常使用  解决方法: 安装System.Drawing.Common的NuGet就能正常使用了 操作之后: 这个是.netcoe中的解决办法,.net framework解决方案中添 ...

随机推荐

  1. Unity的IPostBuildPlayerScriptDLLs:深入解析与实用案例

    Unity IPostBuildPlayerScriptDLLs Unity IPostBuildPlayerScriptDLLs是Unity引擎中的一个非常有用的功能,它可以让开发者在构建项目后自定 ...

  2. 即构发布 LCEP 产品「RoomKit」 ,实现房间内0代码接入

    2021年2月5日,即构科技正式发布全新形态「低代码互动平台」(Low-code Engagement Platform,简称LCEP)产品「RoomKit」. RoomKit定位为低代码互动平台(L ...

  3. Starting Tomcat v8.0 Server at localhost has encountered a problem.

    现有Tomcat文件夹配置有问题,安装新的tomcat从新启动就好了

  4. 2023-07-14:讲一讲Kafka与RocketMQ中存储设计的异同?

    2023-07-14:讲一讲Kafka与RocketMQ中存储设计的异同? 答案2023-07-14: 在Kafka中,文件的布局采用了Topic/Partition的方式,每个分区对应一个物理文件夹 ...

  5. js实现图片预览翻页

    原文地址 可以直接复制粘贴打开,图片是在线的,原理简单好懂! 效果 源码 <!DOCTYPE html> <html> <!--JQuery在线引用--> < ...

  6. Oracle备份与还原(实用版)

    Oracle备份与还原 EXP和IMP是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用. EXPDP和IMPDP是服务端的工具程序,他们只能在ORACLE服务端使用,不能在客户端使用. I ...

  7. Centos7中Jar快速启动脚本

    Centos7中Jar快速启动脚本 创建一个文本,将以下脚本内容复制到文本当中,重命名文本后缀为.sh 注意:根据自己的项目进行更改相关内容,对应注释已说明 #!/bin/sh APP_NAME=ma ...

  8. 解密Prompt系列12. LLM Agent零微调范式 ReAct & Self Ask

    前三章我们分别介绍了思维链的使用,原理和在小模型上的使用.这一章我们正式进入应用层面,聊聊如何把思维链和工具使用结合得到人工智能代理. 要回答我们为什么需要AI代理?代理可以解决哪些问题?可以有以下两 ...

  9. JavaWeb和MVC三层架构

    JavaWeb 概述 网站发布和部署一定要依托技术语言吗: 不一定,一个网站可以直接发布和部署,因为因为浏览器能够识别网页只需要两样东西,网络和静态页面,还有一个装在他们的容器,比如 nginx. 静 ...

  10. 合宙ESP32C3使用PlatformIO开发点亮ST7735S

    开发背景 模块使用的合宙的ESP32-C3(经典款) 购买连接 CORE ESP32核心板是基于乐鑫ESP32-C3进行设计的一款核心板,尺寸仅有21mm*51mm,板边采用邮票孔设计,方便开发者在不 ...