Runtime: ERROR:The type initializer for 'Gdip' threw an exception on Ubunto server

Created on 22 Aug 2018  路  24Comments  路  Source: dotnet/runtime

Tried using System.Drawing.Common package for my .net core web api project. It runs fine under windows but on ubunto (which is my host server), it throws following errors:
This is the method i used to generate thumbnail:

public static bool CreateThumbnail(int Width, int Height, Stream filePath, string saveFilePath)
        {

            try
            {
                var byteArray = filePath;  
                var streamImg = Image.FromStream(byteArray);

                Bitmap sourceImage = new Bitmap(streamImg);
                using (Bitmap objBitmap = new Bitmap(Width, Height))
                {
                    objBitmap.SetResolution(sourceImage.HorizontalResolution, sourceImage.VerticalResolution);
                    using (Graphics objGraphics = Graphics.FromImage(objBitmap))
                    {
                        // Set the graphic format for better result cropping   
                        objGraphics.SmoothingMode = SmoothingMode.HighQuality;
                        objGraphics.PixelOffsetMode = PixelOffsetMode.HighSpeed;
                        objGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        objGraphics.CompositingQuality = CompositingQuality.HighQuality;
                        objGraphics.DrawImage(sourceImage, 0, 0, Width, Height);

                        // Save the file path, note we use png format to support png file   
                        objBitmap.Save(saveFilePath);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Log("Create Thumbnail: ERROR:" + ex.Message + "\n" + ex.StackTrace);
                return false;
            }
            return true;
        }
ERROR:The type initializer for 'Gdip' threw an exception.
at System.Drawing.SafeNativeMethods.Gdip.GdipLoadImageFromDelegate_linux(StreamGetHeaderDelegate getHeader, StreamGetBytesDelegate getBytes, StreamPutBytesDelegate putBytes, StreamSeekDelegate doSeek, StreamCloseDelegate close, StreamSizeDelegate size, IntPtr& image)
at System.Drawing.Image.InitFromStream(Stream stream)
at System.Drawing.Image.LoadFromStream(Stream stream, Boolean keepAlive)

Already installed
sudo apt-get install libgdiplus
and did this too:

cd /usr/lib
sudo ln -s libgdiplus.so gdiplus.dll

So Any workaround or thoughts on this?

area-System.Drawing needs more info

Most helpful comment

Installed libc6-dev and everything works
https://github.com/JanKallman/EPPlus/issues/83

All 24 comments

cd /usr/lib
sudo ln -s libgdiplus.so gdiplus.dll

Did you try without doing this step? This shouldn't be necessary as we do search for libgdiplus.so in Unix.

We only try to load gdiplus.dll when running in Windows.

First tried without

cd /usr/lib
sudo ln -s libgdiplus.so gdiplus.dll

but it didn;t work so used above solution but none of them work either.

Could you try adding this path to LD_LIBRARY_PATH?

export LD_LIBRARY_PATH=/usr/lib

And also could you confirm that libgdiplus.so is present under /usr/lib?

If that doesn't work, would you mind running a little c app just to see if you can load libgdiplus there?

#include <dlfcn.h>
#include <stdio.h>
int main(void)
{
  void* ptr = dlopen("libgdiplus.so", 0x002);
  printf("Found: %s\n", ptr != 0 ? "yes" : "no");
  return 0;
}

@softsan any luck?

Sorry for late reply.
System.Drawing.Common didn't work for me so i gave it up(since it was urgent and didn't get time to experiments).
so I used CoreCompact.System.Drawing library which resolved my issue.
Thanks.

Ok, I just wanted to understand what was happening to fix it if it was a real product issue. I'm pretty sure that setting the LD_LIBRARY_PATH env variable correctly should fix your issue. Whenever you have time would you mind checking and if it still fails, I would love to dive into the issue and fix it.

Will close the issue in the meantime, please feel free to re-open and I'm sorry you where hitting this.

Hey
I also experience the same issue
I tried all of the above solutions, even with the export LD_LIBRARY_PATH=/usr/lib

Noob question: do I need to restart server or something else? (I just restarted the service by running dotnet run)

I tried to run your code

include

include

int main(void)
{
void* ptr = dlopen("libgdiplus.so", 0x002);
printf("Found: %s\n", ptr != 0 ? "yes" : "no");
return 0;
}

And here is the result

screen shot 2018-11-12 at 11 36 07 am

Did you try installing libgdiplus?

And also could you confirm that libgdiplus.so is present under /usr/lib?

What OS is your server running?

Hi,

I've got the same problem.

  1. Installed libgdiplus
  2. I can find it under /usr/lib
  3. sudo ln -s libgdiplus.so gdiplus.dll
  4. export LD_LIBRARY_PATH=/usr/lib
  5. Restart service and still

The type initializer for 'Gdip' threw an exception.

I'll try to use CoreCompact.System.Drawing as softsan wrote

Installed libc6-dev and everything works
https://github.com/JanKallman/EPPlus/issues/83

Hello guys, someone please help me to fix this error,

I've always got "The type initializer for 'Gdip' threw an exception." when i deploy my .NetCore API in ubuntu server. But the codes is working in my local machine (Windows).

I'm trying to get a image icon in my server folder named pictures/3FALogo.ico

12

23

I've always got this error response in my ubuntu server when accessing the API endpoint.
54

Someone can help me?
Thank you.

Have you installed libc6-dev on linux? For me it was the problem.

Got same issue:
I am using CentOS 7 and .NET Core 2.2, using System.Drawing.Common package, got same error: "error: The type initializer for 'Gdip' threw an exception.".
And I tried to use sudo yum install libgdiplus, it looks good but I cannot find neither libgdiplus.so, nor gdiplus.dll in the folder.

BTW, it's working fine on my local Windows 10 when debugging, but does not working on CentOS.
Could someone help me out? Thank you.

Fixed
I use # find -name libgdiplus* and find the files exists in /usr/lib64 instead of /usr/lib, so I follow the instructions after replacing the correct folder /usr/lib64. It works!
Thank for the instructions here.

In case someone is running into the same issue: on macOS, I had the "The type initializer for 'Gdip' threw an exception." exception despite having mono-libgdiplus installed via HomeBrew.

The reason was that my target framework was already netcoreapp3.0, but my System.Drawing.Common package was 4.5.1. Updating to 4.6.0-preview8.19405.3 fixed it.

In my case on Raspbian running:
sudo apt-get install -y libgdiplus
and restarting solved the issue.

I have tried all of the solutions above, but none seem to have worked for me yet. I'm running an Ubuntu Mini 18.04 server, using System.Drawing.Common 4.7.0, have installed libgdiplus AND libc6-dev, rebooted the server and still am getting the same Exception. Have been working on this all day, haven't been able to crack it yet. Help would be appreciated :D

Edit: My application creates Bitmaps from a png source (logo). Judging by the TODO list of the people behind libgdiplus, that is a feature yet to be implemented: https://github.com/mono/libgdiplus/blob/master/TODO

Can confirm that In a fresh ubuntu 18.04 with dotnetcore 3.1, System.Drawing.Common package 4.7.0
It worked me after installing
sudo apt-get install libgdiplus

I'm running at mac os 10.15.4, using System.Drawing.Common 4.7.0,
I have installed libgdiplus(libgidplus.dylib in PATH /usr/local/lib),
but also throw the same Exception

Same problem here on macOS 10.15.5, using System.Drawing.Common version 4.7.0 and mono-libgdiplus (installed via Homebrew).

.net Core 3.1 | Docker | alpine-3.1.1 | linux container on windows
for me it worked when I manually did
apk add libgdiplus --no-cache --repository http://dl-3.alpinelinux.org/alpine/edge/testing/ --allow-untrusted

and then

export LD_LIBRARY_PATH=/usr/lib

But.. it only works when ssh into container! It won't work for me inside the dockerfile.

I'm getting similar too.

Was running into this issue inside my docker container.
Added
RUN apt-get install libgdiplus -y
to my Dockerfile and fixed the issue for me.

For anyone else having this issue in the mcr.microsoft.com/dotnet/core/aspnet:3.1 image, lonwern/aspnetcore-libgdiplus appears to fix the issue with

RUN apt-get update \
    && apt-get install -y --no-install-recommends libgdiplus libc6-dev \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*
Was this page helpful?
0 / 5 - 0 ratings