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

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?

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 24 (5 by maintainers)

Commits related to this issue

Most upvoted comments

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

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

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.

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 running into this issue inside my docker container. Added RUN apt-get install libgdiplus -y to my Dockerfile and fixed the issue for me.

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

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

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

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