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. Python Django 零基础从零到一部署服务,Hello Django!全文件夹目录和核心代码!

    在这篇文章中,我将手把手地教你如何从零开始部署一个使用Django框架的Python服务.无论你是一个刚开始接触开发的新手,还是一个有经验的开发者想要快速了解Django,这篇教程都会为你提供一条清晰 ...

  2. APP流水线测试领域探索与最佳实践

    1 背景 APP端UI自动化因其特殊性(需连接测试机)一般都在本地执行,这种执行方式的局限性有以下弊端: 时效性低:研发每次打包后都需要通知测试,测试再去打包平台取包,存在时间差 研发自测或产品验收无 ...

  3. Unity的IGenerateNativePluginsForAssemblies:深入解析与实用案例

    Unity IGenerateNativePluginsForAssemblies Unity是一款非常流行的游戏引擎,它支持多种平台,包括Windows.Mac.Linux.Android.iOS等 ...

  4. 自研ORM 子查询&嵌套查询

    作者 Mr-zhong 代码改变世界.... 一.前言 Fast Framework 基于NET6.0 封装的轻量级 ORM 框架 支持多种数据库 SqlServer Oracle MySql Pos ...

  5. Prometheus-3:一文详解promQL

    读前提示: 本文字数较多且紧凑,最好预留15min一次性看完,好营养,易吸收. promQL详解 Prometheus提供了内置的数据查询语言PromQL(全称为Prometheus Query La ...

  6. 为什么使用ioutil.ReadAll 函数需要注意

    1. 引言 当我们需要将数据一次性加载到内存中,ioutil.ReadAll 函数是一个方便的选择,但是ioutil.ReadAll 的使用是需要注意的. 在这篇文章中,我们将首先对ioutil.Re ...

  7. 暗黑王者|ZEGO 低照度图像增强技术解析

    在低光照的夜间,摄像头采集的画面通常是一片昏暗,画面清晰度要远远低于肉眼.而随着实时音视频应用技术的发展,我们已经看到了各种画质增强的视频增强技术,那么是否存在一种技术,可以使视频在低光照条件下看起来 ...

  8. modulemap的使用方法

    modulemap的作用   modulemap 文件是用来解决 C,Object-C,C++ 代码在 Swift 项目中集成的问题的. 在 Swift 项目中,如果需要使用 C,Object-C 或 ...

  9. K8S | Deployment应用编排

    目录 一.背景 二.Deployment组件 1.简介 2.语法说明 三.基础用例 1.创建操作 2.查看信息 3.更新操作 4.删除操作 四.进阶用例 1.回滚操作 2.伸缩操作 3.暂停与恢复 五 ...

  10. 我开源了团队内部基于SpringBoot Web快速开发的API脚手架v1.6.0更新

    什么是 rest-api-spring-boot-starter rest-api-spring-boot-starter 适用于SpringBoot Web API 快速构建让开发人员快速构建统一规 ...