这是我将图片添加到工作表的代码。我从数据库中以字节形式获取图片。 .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'的类型初始值设定项引发了异常”
我在google上搜索了很多次。一般建议采用这种方式添加docker文件:
RUN apt-get install libgdiplus
但是这种方式也没有解决我的问题。有人可以帮助我吗?
提前致谢。
我有一个像这样的 Dockerfile:
FROM mcr.microsoft.com/dotnet/aspnet:7.0 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:7.0 AS build
WORKDIR /src
COPY ...
.
.
.
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "...dll"]
我正在第二行运行
apt-get update and install
命令。你可以像这样运行你的命令。我希望它也适合你。
修复了 ClosedXML 0.96.0、.NET 6 和 Alpine Linux 在创建 Excel 文件时引发以下异常的问题:
“Gdip”的类型初始值设定项引发异常
添加
libgdiplus
包
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
...
启用对非 Windows 平台的
System.Drawing
支持:(参考):
在您的项目文件 (
*.csproj
) 中,添加:
<ItemGroup>
<RuntimeHostConfigurationOption Include="System.Drawing.EnableUnixSupport" Value="true" />
</ItemGroup
最受接受的答案在 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 Agent 进行部署):
这是我的 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 包:“System.Drawing.Common”
我在Linux服务器上部署时遇到这个问题
尝试一下:
sudo apt-get install -y libgdiplus
并重新启动服务器
sudo systemctl restart nginx
尝试在项目中安装 libgdi NuGet 包。请参阅:https://github.com/eugeneereno/libgdiplus-packaging
dotnet add package ereno.linux-x64.eugeneereno.System.Drawing
Mac 用户请参阅:https://github.com/eugeneereno/libgdiplus-packaging
在 docker 文件中添加该代码以安装 libgdi。
RUN apt-get update && apt-get install -y libgdiplus
之后,在 .csproj 文件中添加该代码以启用 unix 支持。
<ItemGroup>
<RuntimeHostConfigurationOption Include="System.Drawing.EnableUnixSupport" Value="true" />
</ItemGroup>
并成为胜利的伙伴!