c# - System.drawing.common 'gdip' 的类型初始值设定项引发异常

标签 c# kubernetes .net-core worksheet closedxml

这是我将图片添加到工作表的代码。我从数据库中以字节的形式获取图片。 .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));
            }
        }
frameworks
我的 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
但是这种方式也没有解决我的问题。有谁能够帮助我?
提前致谢。

最佳答案

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

关于c# - System.drawing.common 'gdip' 的类型初始值设定项引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67091719/

相关文章:

kubernetes - Helm 列表 : cannot list configmaps in the namespace "kube-system"

kubernetes - 无法使用新上下文连接到 Kubernetes

c# - 匹配参数的可空性和返回类型的泛型类型参数

visual-studio - 在 .Net Core 中创建自定义 Sdk

c# - MSWord 中的表格对齐

c# - 通过引用传递 IDisposable 对象会导致错误?

amazon-web-services - 错误 : getting availability zones when trying to create EKS cluster

c# - Controller 可以接受空参数值吗?

c# - 将序列化对象迁移到新版本

c# - 正则表达式:找到整数但不是 float