首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >System.drawing.common‘’的类型初始值设定项引发异常

System.drawing.common‘’的类型初始值设定项引发异常
EN

Stack Overflow用户
提问于 2021-04-14 20:24:25
回答 2查看 3.6K关注 0票数 6

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

代码语言:javascript
复制
        [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服务器信息如下:

代码语言:javascript
复制
System.drawing.common the type initializer for 'gdip' threw an exception
代码语言:javascript
复制
*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‘的类型初始化器抛出了一个异常“

我在谷歌上搜索了很多次。通常建议使用这种方式来添加docker文件:

代码语言:javascript
复制
RUN apt-get install libgdiplus

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

提前谢谢。

EN

回答 2

Stack Overflow用户

发布于 2021-04-14 21:06:47

我有一个这样的Dockerfile:

代码语言:javascript
复制
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命令。您可以像这样运行命令。我希望它也适用于你。

票数 13
EN

Stack Overflow用户

发布于 2021-11-25 06:20:22

最被接受的答案对我来说在ubuntu20.04上不起作用,而且看起来对非.NET操作系统的支持在ubuntu6中被移除了:

System.Drawing.Common only supported on Windows

建议的替代方案是:

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

ImageSharp https://github.com/SixLabors/ImageSharp SkiaSharp https://github.com/mono/SkiaSharp Microsoft.Maui.Graphics https://github.com/dotnet/Microsoft.Maui.Graphics

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67091719

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档