Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
softsan opened this issue Aug 22, 2018 · 24 comments
Closed

Comments

@softsan
Copy link

softsan commented Aug 22, 2018

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?

@safern
Copy link
Member

safern commented Aug 22, 2018

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.

@softsan
Copy link
Author

softsan commented Aug 23, 2018

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.

@safern
Copy link
Member

safern commented Aug 23, 2018

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;
}

@safern
Copy link
Member

safern commented Aug 27, 2018

@softsan any luck?

@softsan
Copy link
Author

softsan commented Aug 29, 2018

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.

@safern
Copy link
Member

safern commented Aug 29, 2018

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.

@safern safern closed this as completed Aug 29, 2018
@kevinnguyen9185
Copy link

kevinnguyen9185 commented Nov 12, 2018

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 <dlfcn.h>
#include <stdio.h>
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

@safern
Copy link
Member

safern commented Nov 26, 2018

Did you try installing libgdiplus?

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

What OS is your server running?

@LHeczko
Copy link

LHeczko commented Jan 10, 2019

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

@LHeczko
Copy link

LHeczko commented Jan 10, 2019

Installed libc6-dev and everything works
JanKallman/EPPlus#83

@jlestercastro
Copy link

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.

@LHeczko
Copy link

LHeczko commented Apr 29, 2019

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

@sw0
Copy link

sw0 commented Jun 9, 2019

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.

@sw0
Copy link

sw0 commented Jun 9, 2019

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.

@chucker
Copy link

chucker commented Aug 29, 2019

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.

@pawelpetruch
Copy link

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

@EnaitForza
Copy link

EnaitForza commented Feb 17, 2020

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

@GayanSandaruwan
Copy link

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

@mrpanwei
Copy link

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

@alexhauser
Copy link

alexhauser commented Jun 12, 2020

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

@drdream
Copy link

drdream commented Jun 17, 2020

.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.

@jhancock4d
Copy link

I'm getting similar too.

@Holmslice86
Copy link

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.

@brookpatten
Copy link

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/*

mythz added a commit to NetCoreApps/Imgur that referenced this issue Dec 3, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests