0
点赞
收藏
分享

微信扫一扫

Net Core通过GDI+在CentOS 7(Docker)环境中绘图报错The type initializer for ‘Gdip‘ threw an exception

读思意行 2022-06-13 阅读 159

目录

  • ​​一、前言​​
  • ​​二、问题分析​​
  • ​​三、问题处理​​
  • ​​四、优化​​
  • ​​五、构建一个带libgdiplus的DotNetCore基础镜像​​

 

一、前言

今天在AspNetCore3.1环境中做了一个用户登录页面,在登录页面中有一个功能就是需要后端动态绘制一个验证码图片,防止前端通过机器或爬虫工具模拟自动登录。
在开发机器上(windows10)调试正常,但是部署到centos7容器(容器基础环境mcr.microsoft.com/dotnet/aspnet:3.1)中,验证码一直显示不出来,通过前端调试发现请求500(服务器内部错误)
Net Core通过GDI+在CentOS 7(Docker)环境中绘图报错The type initializer for ‘Gdip‘ threw an exception_gdi+
然后就登录服务器查看容器日志
果然报错“'Gdip'的类型初始化器抛出了一个异常”
Net Core通过GDI+在CentOS 7(Docker)环境中绘图报错The type initializer for ‘Gdip‘ threw an exception_docker_02

代码如下:

/// <summary>
/// 生成验证码
/// </summary>
/// <returns></returns>
public static byte[] ValidateCode(string code)
{
using (var bitmap = new Bitmap(100, 30))
{
using (var gph = Graphics.FromImage(bitmap))
{
gph.FillRectangle(Brushes.White, 0, 0, bitmap.Width, bitmap.Height);
gph.DrawRectangle(new Pen(Color.Black), 1, 1, bitmap.Width - 2, bitmap.Height - 2);
using (Brush bush = new SolidBrush(Color.SteelBlue))
{
gph.DrawString(code, new Font("黑体", 20, FontStyle.Italic), bush, 10, 2);
var random = new Random();
// 画线条
for (int i = 0; i < 5; i++)
{
gph.DrawLine(new Pen(GetRandomColor()), random.Next(bitmap.Width), random.Next(bitmap.Height), random.Next(bitmap.Width), random.Next(bitmap.Height));
}

// 画躁点
for (int i = 0; i < 100; i++)
{
bitmap.SetPixel(random.Next(bitmap.Width), random.Next(bitmap.Height), GetRandomColor());
}
using (var ms = new MemoryStream())
{
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
var imgData = ms.GetBuffer();
return imgData;
}
}
}
}
}

二、问题分析

查看相关资料发现.Net Core本身不包括和图片有关的Image、Bitmap等类型。用过.Net框架的同学应该都知道Bitmap、Image是放在​​System.Drawing.dll​​​中,通过COM引用就可使用。
但在.Net Core中对于图片的操作在我们开发中很常见,比如:生成验证码、二维码等等。在.NET Core 的早期版本中,也有.NET社区开发者实现了一些 ​​​System.Drawing​​​的Image等类型实现的组件,比如​​CoreCompat.System.Drawing​​​、​​ZKWeb.System.Drawing​​​等。后来微软官方提供了一个组件​​System.Drawing.Common​​​实现了​​System.Drawing​​​的常用类型,以Nuget 包的方式发布的。然后我当前的项目的验证码绘制就是使用的微软官方的​​System.Drawing.Common​​。

​System.Drawing.Common​​​组件提供对GDI+图形功能的访问。它是依赖于GDI+的,那么在Linux上它如何使用GDI+,因为Linux上是没有GDI+的。Mono团队使用C语言实现了GDI+接口,提供对非Windows系统的GDI+接口访问能力(个人认为是模拟GDI+,与系统图像接口对接),这个就是​​libgdiplus​​​。进而可以推测 System.Drawing.Common 这个组件实现时,对于非Windows系统肯定依赖了​​ligdiplus​​这个组件。如果我们当前系统不存在这个组件,那么自然会报错,找不到它,安装它即可解决。

三、问题处理

首先进入Gdip报错项目容器中安装​​libgdiplus​

  1. 更新基础镜像(mcr.microsoft.com/dotnet/aspnet:3.1)中的apt-get应用程序管理器,这一步务必需要更新哦,不然会报找不到​​libgdiplus​​​:​​apt-get update -y​​​Net Core通过GDI+在CentOS 7(Docker)环境中绘图报错The type initializer for ‘Gdip‘ threw an exception_docker_03
  2. 安装libgdiplus:​​apt-get install -y libgdiplus​​​Net Core通过GDI+在CentOS 7(Docker)环境中绘图报错The type initializer for ‘Gdip‘ threw an exception_.net_04
  3. 创建符号链接:​​ln -s /usr/lib/libgdiplus.so /usr/lib/gdiplus.dll​​​Net Core通过GDI+在CentOS 7(Docker)环境中绘图报错The type initializer for ‘Gdip‘ threw an exception_docker_05
  4. 重启容器
    Net Core通过GDI+在CentOS 7(Docker)环境中绘图报错The type initializer for ‘Gdip‘ threw an exception_gdi+_06

这时我们再访问一下发现可以了
Net Core通过GDI+在CentOS 7(Docker)环境中绘图报错The type initializer for ‘Gdip‘ threw an exception_docker_07

四、优化

由于当前项目基于gitlab+jenkins做了持续部署,但在下载安装​​libgdiplus​​​时发现使用的软件包源又是国外的地址,所以造成我们使用国内网络非常慢,进而造成整体构建过程非常慢。
如果在​​​Dockerfile​​中这么写

FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
WORKDIR /app
COPY . .
RUN apt-get update -y && apt-get install -y libgdiplus && apt-get clean && ln -s /usr/lib/libgdiplus.so /usr/lib/gdiplus.dll
...
...

那么当提交代码后整个构建时间就会非常长,我们可以基于mcr.microsoft.com/dotnet/aspnet:3.1基础镜像构建一个带libgdiplus的自定义镜像。然后Dockerfile中就基于该镜像构建就可以了。

五、构建一个带libgdiplus的DotNetCore基础镜像

  1. 通过docker拉取一个.netcore3.1基础镜像:​​docker pull mcr.microsoft.com/dotnet/aspnet:3.1​Net Core通过GDI+在CentOS 7(Docker)环境中绘图报错The type initializer for ‘Gdip‘ threw an exception_docker_08
  2. 进入容器部署libgdiplus(步骤和上面一样)


apt-get update -y
apt-get install -y libgdiplus
apt-get clean
ln -s /usr/lib/libgdiplus.so /usr/lib/gdiplus.dll


Net Core通过GDI+在CentOS 7(Docker)环境中绘图报错The type initializer for ‘Gdip‘ threw an exception_gdi+_09

  1. 经过漫长的部署,成功后我们退出当前容器并将当前容器重新打包成一个新的镜像:​​docker commit -a="simple" -m="added libgdiplus based on .netcore3.1" 28a66ebccd55 dotnetcore-with-libgdiplus:v3.1​Net Core通过GDI+在CentOS 7(Docker)环境中绘图报错The type initializer for ‘Gdip‘ threw an exception_docker_10
    Net Core通过GDI+在CentOS 7(Docker)环境中绘图报错The type initializer for ‘Gdip‘ threw an exception_docker_11
  2. 修改项目Dockerfile基础镜像为刚刚构建的自定义镜像​​dotnetcore-with-libgdiplus:v3.1​Net Core通过GDI+在CentOS 7(Docker)环境中绘图报错The type initializer for ‘Gdip‘ threw an exception_docker_12
  3. 提交代码查看jenkins构建日志,通过截图发现构建时已经成功使用我们自定义打包的基础镜像(dotnetcore-with-libgdiplus:v3.1)
    Net Core通过GDI+在CentOS 7(Docker)环境中绘图报错The type initializer for ‘Gdip‘ threw an exception_docker_13
  4. 查看项目看是否可以正常通过GDI+在CentOS 7(Docker)环境中绘图
    Net Core通过GDI+在CentOS 7(Docker)环境中绘图报错The type initializer for ‘Gdip‘ threw an exception_docker_14

至此就全部大功告成了,完美解决.Net Core通过GDI+在CentOS 7(Docker)环境中绘图报错The type initializer for 'Gdip' threw an exception的问题

 


举报

相关推荐

0 条评论