出现问题的原因

在Linux环境部署.NET Core程序时,如果要到System.Drawing.Common引用会出现该问题,目前大量的第三方组件使用该Windows专用库,尤其是涉及图片处理、Word相关的组件、二维码等

问题现象

出现相关Gdip异常,安装相应组件后重启项目及服务器无效,异常如下

The type initializer for 'Gdip' threw an exception.
at System.Drawing.SafeNativeMethods.Gdip.GdipGetGenericFontFamilySansSerif(IntPtr& fontfamily)
at System.Drawing.FontFamily.GetGdipGenericSansSerif()
at System.Drawing.FontFamily.get_GenericSansSerif()
at System.Drawing.Font.CreateFont(String familyName, Single emSize, FontStyle style, GraphicsUnit unit, Byte charSet, Boolean isVertical)
at System.Drawing.Font..ctor(String familyName, Single emSize, FontStyle style, GraphicsUnit unit, Byte gdiCharSet, Boolean gdiVerticalFont)
at OfficeOpenXml.ExcelRangeBase.AutoFitColumns(Double MinimumWidth, Double MaximumWidth)
at OfficeOpenXml.ExcelRangeBase.AutoFitColumns(Double MinimumWidth)
at OfficeOpenXml.ExcelRangeBase.AutoFitColumns()
at Magicodes.ExporterAndImporter.Excel.Utility.ExportHelper`1.AddHeaderAndStyles()
at Magicodes.ExporterAndImporter.Excel.Utility.ExportHelper`1.Export(ICollection`1 dataItems)
at Magicodes.ExporterAndImporter.Excel.ExcelExporter.ExportAsByteArray[T](ICollection`1 dataItems)

解决办法


.NET 6之前

在Linux服务器上安装 libgdiplus 即可解决

libgdiplusSystem.Drawing.Common原生端跨平台实现的主要提供者,是开源mono项目

安装步骤(以Centos 7为例)

  • 更新包

    yum install -y epel-release
  • 查找包
    yum whatprovides libgdiplus
    
    # 输出以下内容,具体以显示为准,注意版本号
    # libgdiplus-2.10-10.el7.x86_64 : An Open Source implementation of the GDI+ API.
    • ImageSharp
    • SkiaSharp
    • Microsoft.Maui.Graphics安装包
      yum install -y libgdiplus-2.10-10.el7.x86_64

      .NET 6及以后

      由于官方不再支持在非Windows环境使用libgdiplus,需要单独开启运行时环境支持

      处理步骤

      • 按照.NET 6之前的方案安装 libgdiplus
      • runtimeconfig.json配置文件中新增System.Drawing.EnableUnixSupport
        {
        "runtimeOptions": {
        "configProperties": {
        "System.Drawing.EnableUnixSupport": true
        }
        }
        }

        构建项目时,会在输出目录中生成[appname].runtimeconfig.json文件,只需要修改该配置文件即可

      其他建议

      官方不再建议使用libgdiplus,主要是原因是libgdiplus非常臃肿、外部依赖众多、未解决bug及异常情况难以修复,修改[appname].runtimeconfig.json只是临时方案,需要逐步迁移到以下项目

      官方参考资料:https://docs.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/system-drawing-common-windows-only

.NET 6 使用 System.Drawing.Common 出现 The type initializer for ‘Gdip’ threw an exception 异常的解决办法的更多相关文章

  1. System.Drawing Linux Centos7 The type initializer for 'Gdip' threw an exception

    System.Drawing 在linux使用时提示异常 The type initializer for 'Gdip' threw an exception 解决方案: yum install au ...

  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. Linux/Docker 中使用 System.Drawing.Common 踩坑小计

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

  9. 【CodeSmith】The System.Data.SQLite library is not installed on this computer,不能使用SQLite解决办法

    1.出现问题原因 1)System.Data.SQLite 尚未安装在您的计算机上 2)System.Data.SQLite 尚未正确配置 2.下载并安装System.Data.Sqlite [注意] ...

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

随机推荐

  1. 【主流技术】聊一聊 Redis 的基本结构和简单应用(一)

    目录 前言 一.String 类型 二.List 类型 三.Hash 类型 四.Set 结构 五.Sort Set (Zset)结构 六.文章小结 前言 Redis 是目前互联网后端的热门中间件之一, ...

  2. STM32CubeMX教程16 DAC - 输出3.3V内任意电压

    1.准备材料 开发板(正点原子stm32f407探索者开发板V2.4) STM32CubeMX软件(Version 6.10.0) keil µVision5 IDE(MDK-Arm) ST-LINK ...

  3. 灵魂拷问std::enable_shared_from_this,揭秘实现原理

    参考博客: std::enable_shared_from_this原理浅析 引言 在C++编程中,使用智能指针是一种安全管理对象生命周期的方式.std::shared_ptr是一种允许多个指针共享对 ...

  4. 在Windows上D盘上安装Docker

    Reference https://www.willh.cn/articles/2022/07/13/1657676401964.html Docker默认安装在C盘: "C:\Progra ...

  5. Codeforces 1312B Bogosort (逆序证明)

    Example input 3 1 7 4 1 1 3 5 6 3 2 1 5 6 4 output 7 1 5 1 3 2 4 6 1 3 5 看题的时候发现和sort有关,但一定要逆序排序 证明: ...

  6. SpringCloud学习 系列一、 前言-为什么要学习微服务

    系列导航 SpringCloud学习 系列一. 前言-为什么要学习微服务 SpringCloud学习 系列二. 简介 SpringCloud学习 系列三. 创建一个没有使用springCloud的服务 ...

  7. 1、springboot工程新建(单模块)

    系列导航 springBoot项目打jar包 1.springboot工程新建(单模块) 2.springboot创建多模块工程 3.springboot连接数据库 4.SpringBoot连接数据库 ...

  8. python global函数的使用

    1.在全局变量与局部变量均存在时自定义的函数优先使用局部变量,自定义函数并不能改变全局变量的值. 查看运行结果:  2.在没有局部变量时,使用全局变量,且函数内部不能改变全局变量的值  查看运行结果: ...

  9. python常见面试题讲解(十一)字符串反转-五种解法

    题目描述 写出一个程序,接受一个字符串,然后输出该字符串反转后的字符串.(字符串长度不超过1000) 输入描述: 输入N个字符 输出描述: 输出该字符串反转后的字符串 示例1 输入 abcd 输出 d ...

  10. MyBatis 系列:MyBatis 源码环境搭建

    目录 一.环境准备 二.下载 MyBatis 源码和 MyBatis-Parent 源码 三.创建空项目.导入项目 四.编译 mybatis-parent 五.编译 mybatis 六.测试 总结 一 ...