【问题标题】:System.drawing.common the type initializer for 'gdip' threw an exceptionSystem.drawing.common 'gdip' 的类型初始化器抛出异常
【发布时间】:2021-07-09 12:02:55
【问题描述】:

这是我将图片添加到工作表的代码。我从数据库中获取图片作为一个字节。 .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));
            }
        }

我的 Kubernetes 服务器信息如下:

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

但是这种方式也没有解决我的问题。有人可以帮帮我吗?

提前致谢。

【问题讨论】:

  • 这是 windows 还是 linux 容器?
  • 你可以使用这个repo来生成简单的excel文件:github.com/doxa-labs/ExcelLabs
  • 你也需要 libc6-dev。检查它是否已安装。

标签: c# kubernetes .net-core worksheet closedxml


【解决方案1】:

我有一个像这样的 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 命令。你可以像这样运行你的命令。我希望它也对你有用。

【讨论】:

  • apt-utils libgdiplus libc6-dev 位成功了;谢谢!
  • 工作的伙伴,谢谢
【解决方案2】:

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

System.Drawing.Common only supported on Windows

建议的替代方案是:

要将这些 API 用于跨平台应用,请迁移到其中一个 以下库:

ImageSharphttps://github.com/SixLabors/ImageSharp

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

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

【讨论】:

  • 从 .NET 6 开始,这是最好的答案,因为它还包括一个特定的 Microsoft 解决方法,即使不建议使用 .NET 6 也可以继续使用 System.Drawing.Common
猜你喜欢
  • 2011-04-16
  • 1970-01-01
  • 1970-01-01
  • 2018-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多