繁体   English   中英

System.drawing.common 'gdip' 的类型初始值设定项抛出异常

[英]System.drawing.common the type initializer for 'gdip' threw an exception

这是我将图片添加到工作表的代码。 我从数据库中以字节形式获取图片。 .Net Core框架版本为2.2.104。 这是一个 API 项目。 在我的语言环境中,代码运行良好。 我使用 ClosedXML 组件 0.95.4 版本如下。

[HttpPost("GetTowel")]
public IActionResult GetTowel()
{
    string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    string fileName = "Towel Quotation.xlsx";
    try
    {
        using (var workbook = new XLWorkbook())
        {
            IXLWorksheet worksheet = workbook.Worksheets.Add("Towel Quotation");
            
            byte[] bytes = _fileService.Get(159).FileInBytes;
            System.IO.Stream x = new System.IO.MemoryStream(bytes);

            //the exception is throwed at this line:
            **var image = worksheet.AddPicture(x).MoveTo(worksheet.Cell("P1")).Scale(1.0);**

            using (var stream = new MemoryStream())
            {
                workbook.SaveAs(stream);
                var content = stream.ToArray();
                return File(content, contentType, fileName);
            }
        }
    }
    catch (Exception ex)
    {
        return BadRequest(ErrorResultFormatter.PrepareErrorResult("",ex.Message));
    }
}

构架

我的 Kube.netes 服务器信息如下:

System.drawing.common the type initializer for 'gdip' threw an exception
*FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build WORKDIR /app
COPY *.csproj Nuget.Config ./ RUN dotnet restore /property:Configuration=Release
--configfile=Nuget.Config --no-cache --force
COPY . ./temp/ WORKDIR /app/temp RUN dotnet publish -c Release -o out FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS runtime ENV ASPNETCORE_URLS="http://+" ENV ASPNETCORE_Kestrel__Certificates__Default__Password="*****" WORKDIR /app COPY --from=build /app/temp/out ./ ENTRYPOINT ["dotnet", "blahblah.dll"]*

在服务器端,我得到如下异常:“system.drawing.common 'gdip' 的类型初始值设定项抛出异常”

我在谷歌上搜索了很多次。 这种方式一般建议添加docker文件:

RUN apt-get install libgdiplus

但是这种方式也没有解决我的问题。 有谁能够帮助我?

提前致谢。

我有一个像这样的 Dockerfile:

FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base
RUN apt-get update && apt-get install -y apt-utils libgdiplus libc6-dev

WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY ...

我在第二行运行apt-get update and install命令。 你可以像这样运行你的命令。 我希望它也适合你。

在 ubuntu 20.04 上,最被接受的答案对我不起作用,看起来 .NET 6 已删除对非 Windows 操作系统的支持:

System.Drawing.Common 仅在 Windows 上支持

建议的替代方案是:

要将这些 API 用于跨平台应用程序,请迁移到以下库之一:

ImageSharp https://github.com/SixLabors/ImageSharp

SkiaSharp https://github.com/mono/SkiaSharp

Microsoft.Maui.Graphics https://github.com/dotnet/Microsoft.Maui.Graphics

对我来说,以下解决了这个问题(在本地测试以及在 Azure 中使用 Ubuntu 代理的部署中):

这是我的 Dockerfile 中的内容:

  FROM mcr.microsoft.com/dotnet/aspnet:6.0 as base
  RUN apt-get update && apt-get install -y libgdiplus
  WORKDIR /app
  EXPOSE 80

这是我添加到每个 *.csproj 文件中的内容:

  <ItemGroup>
    <RuntimeHostConfigurationOption Include="System.Drawing.EnableUnixSupport" Value="true" />
  </ItemGroup>

而且我还必须安装这个 NuGet package:“ System.Drawing.Common

我在 Linux 服务器上部署时遇到了这个问题

试试看:

sudo apt-get install -y libgdiplus

并重新启动服务器

sudo systemctl restart nginx

修复 ClosedXML 0.96.0 & .NET 6 & alpine linux
在创建 Excel 文件期间抛出异常“'Gdip' 的类型初始化程序引发异常”。

1.) 添加“libgdiplus”package

Dockerfile

FROM mcr.microsoft.com/dotnet/aspnet:6.0-alpine 
RUN apk add libgdiplus --repository http://dl-.alpinelinux.org/alpine/edge/testing/ 
RUN apk add ttf-freefont libssl1.1 
...

2.) 为非 Windows 平台启用“System.Drawing”支持

https://docs.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/system-drawing-common-windows-only#recommended-action

在项目文件 (*.csproj) 中添加

<ItemGroup>
    <RuntimeHostConfigurationOption Include="System.Drawing.EnableUnixSupport" Value="true" />
</ItemGroup

尝试在您的项目中安装 libgdi NuGet package。 见: https://github.com/eugeneereno/libgdiplus-packaging

dotnet add package ereno.linux-x64.eugeneereno.System.Drawing

对于 Mac 用户,请参阅: https://github.com/eugeneereno/libgdiplus-packaging

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM