Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. This is an API project. In my locale, the code works well. I use the ClosedXML component 0.95.4 version as below.

        [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

My Kubernetes server information is below:

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"

I have searched many times on google. That way is suggested generally to add docker file:

RUN apt-get install libgdiplus

But this way also didn't solve my problem. Can anybody help me?

Thanks in advance.

like image 868
Ömer Yalvaç Avatar asked Apr 14 '21 12:04

Ömer Yalvaç


2 Answers

I have a Dockerfile like this:

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. You may run your command like this. I hope it works for you too.

like image 200
fatihyildizhan Avatar answered Oct 27 '22 16:10

fatihyildizhan


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:

System.Drawing.Common only supported on Windows

The suggested alternatives are:

To use these APIs for cross-platform apps, migrate to one of the following libraries:

ImageSharp https://github.com/SixLabors/ImageSharp

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

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

like image 22
onemorecupofcoffee Avatar answered Oct 27 '22 17:10

onemorecupofcoffee