出现问题的原因

在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. Apollo系列之架构设计(一)

    原创文章,转载请标注.https:https://www.cnblogs.com/boycelee/p/17967590 目录 一.什么是配置中心? 二.传统配置有什么问题? 三.配置中心的场景 四. ...

  2. 面试官:请聊一聊String、StringBuilder、StringBuffer三者的区别

    面试官:"小伙子,在日常的写代码过程中,使用过String,StringBuilder和StringBuffer没?" 我:"用过的呀!" 面试官:" ...

  3. Spring 学习笔记(5)AOP

    本文介绍 Spring 中 AOP 的原理及使用方式. Spring AOP 简介 如果说 IoC 是 Spring 的核心,那么面向切面编程就是 Spring 最为重要的功能之一了,在数据库事务中切 ...

  4. 经典Python案例实现

    入门_30个常用python实现 .pdf 一. 二. 三. 四. 五.函数篇 5.1 计算圆的面积 from math import pi as PI def CircleArea(r): if i ...

  5. 体验有礼 | 1 分钟 Serverless 极速部署个人网盘,真网盘真好用!

    你想自己搭一个无敌好用的网盘吗? 想 接着看,还有奖品呢! -- 当前,网盘几乎已成为现代人的标配,而市面上的网盘功能.费用各异,让用户们陷入了对比价格和功能的迷阵中.别对比了,动手吧!作为对存储.流 ...

  6. SetFitABSA: 基于 SetFit 的少样本、方面级情感分析

    SetFitABSA 是一种可以有效从文本中检测方面级情感的技术. 方面级情感分析 (Aspect-Based Sentiment Analysis,ABSA) 是一种检测文本中特定方面的情感的任务. ...

  7. java - 递归排序 - 求数组最小值

    // 递归求数组最小值 public class Bubble { // 定义存储最小值的变量 static int min; public static void main(String[] arg ...

  8. linux环境C语言实现:h264与pcm封装成AVI格式

    ​ 前言 拖了很久的AVI音视频封装实例,花了一天时间终于调完了,兼容性不是太好,但作为参考学习使用应该没有问题. RIFF和AVI以及WAV格式,可以参考前面的一些文章.这里详细介绍将一个H264视 ...

  9. MySQL高可用搭建方案之(MHA)

    有的时候博客内容会有变动,首发博客是最新的,其他博客地址可能会未同步,认准https://blog.zysicyj.top 首发博客地址 原文地址 MHA架构介绍 MHA是Master High Av ...

  10. K8S_IPV6 POD与数据库联通方法以及快速网络调试的一个思路

    K8S_IPV6 POD与数据库联通方法以及快速网络调试的一个思路 背景 前端时间搭建了一套K8S only IPV6 Single Stack的测试环境 因为自己长时间不搞K8S了, 并且IPV6的 ...