[英]System.drawing.common the type initializer for 'gdip' threw an exception
This is my code to add a picture to a worksheet.这是我将图片添加到工作表的代码。 I get the picture as a byte from the database.我从数据库中以字节形式获取图片。 .Net Core framework version is 2.2.104. .Net Core框架版本为2.2.104。 This is an API project.这是一个 API 项目。 In my locale, the code works well.在我的语言环境中,代码运行良好。 I use the ClosedXML component 0.95.4 version as below.我使用 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));
}
}
My Kube.netes server information is below:我的 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"]*
On the server-side, I get the exception as below: "system.drawing.common the type initializer for 'gdip' threw an exception"在服务器端,我得到如下异常:“system.drawing.common 'gdip' 的类型初始值设定项抛出异常”
I have searched many times on google.我在谷歌上搜索了很多次。 That way is suggested generally to add docker file:这种方式一般建议添加docker文件:
RUN apt-get install libgdiplus
But this way also didn't solve my problem.但是这种方式也没有解决我的问题。 Can anybody help me?有谁能够帮助我?
Thanks in advance.提前致谢。
I have a Dockerfile like this:我有一个像这样的 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 ...
I am running apt-get update and install
command on the second line.我在第二行运行apt-get update and install
命令。 You may run your command like this.你可以像这样运行你的命令。 I hope it works for you too.我希望它也适合你。
The most accepted answer did not work for me on ubuntu 20.04 and it looks like support for non-windows os's is removed for .NET 6:在 ubuntu 20.04 上,最被接受的答案对我不起作用,看起来 .NET 6 已删除对非 Windows 操作系统的支持:
System.Drawing.Common only supported on Windows System.Drawing.Common 仅在 Windows 上支持
The suggested alternatives are:建议的替代方案是:
To use these APIs for cross-platform apps, migrate to one of the following libraries:要将这些 API 用于跨平台应用程序,请迁移到以下库之一:
ImageSharp https://github.com/SixLabors/ImageSharp ImageSharp https://github.com/SixLabors/ImageSharp
SkiaSharp https://github.com/mono/SkiaSharp SkiaSharp https://github.com/mono/SkiaSharp
Microsoft.Maui.Graphics https://github.com/dotnet/Microsoft.Maui.Graphics Microsoft.Maui.Graphics https://github.com/dotnet/Microsoft.Maui.Graphics
For me, the following fixed the issue (in local tests and also deployments using Ubuntu Agent in Azure):对我来说,以下解决了这个问题(在本地测试以及在 Azure 中使用 Ubuntu 代理的部署中):
Here is what I have in my Dockerfile:这是我的 Dockerfile 中的内容:
FROM mcr.microsoft.com/dotnet/aspnet:6.0 as base
RUN apt-get update && apt-get install -y libgdiplus
WORKDIR /app
EXPOSE 80
Here is what I have added into every *.csproj file:这是我添加到每个 *.csproj 文件中的内容:
<ItemGroup>
<RuntimeHostConfigurationOption Include="System.Drawing.EnableUnixSupport" Value="true" />
</ItemGroup>
And I also had to install this NuGet package too: " System.Drawing.Common "而且我还必须安装这个 NuGet package:“ System.Drawing.Common ”
I had this problem when deploying on Linux server我在 Linux 服务器上部署时遇到了这个问题
try it:试试看:
sudo apt-get install -y libgdiplus
and restart server并重新启动服务器
sudo systemctl restart nginx
Fix ClosedXML 0.96.0 & .NET 6 & alpine linux修复 ClosedXML 0.96.0 & .NET 6 & alpine linux
throw exception "The type initializer for 'Gdip' threw an exception" durring create Excel file.在创建 Excel 文件期间抛出异常“'Gdip' 的类型初始化程序引发异常”。
1.) Add "libgdiplus" package 1.) 添加“libgdiplus”package
Dockerfile 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.) Enable "System.Drawing" support for non-Windows platforms 2.) 为非 Windows 平台启用“System.Drawing”支持
https://docs.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/system-drawing-common-windows-only#recommended-action https://docs.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/system-drawing-common-windows-only#recommended-action
In project file (*.csproj) add在项目文件 (*.csproj) 中添加
<ItemGroup>
<RuntimeHostConfigurationOption Include="System.Drawing.EnableUnixSupport" Value="true" />
</ItemGroup
Try to install the libgdi NuGet package within your project.尝试在您的项目中安装 libgdi NuGet package。 See: https://github.com/eugeneereno/libgdiplus-packaging见: https://github.com/eugeneereno/libgdiplus-packaging
dotnet add package ereno.linux-x64.eugeneereno.System.Drawing
For Mac users see: https://github.com/eugeneereno/libgdiplus-packaging对于 Mac 用户,请参阅: https://github.com/eugeneereno/libgdiplus-packaging
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.