runtime: System.Drawing on CentOS: Unable to load DLL 'libdl'

Any code which uses System.Drawing fails on CentOS 7.4 with the following exception:

Unhandled Exception: System.TypeInitializationException: The type initializer for 'Gdip' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'libdl': The specified module or one of its dependencies could not be found.
 (Exception from HRESULT: 0x8007007E)
   at Interop.Libdl.dlopen(String fileName, Int32 flag)
   at System.Drawing.SafeNativeMethods.Gdip.LoadNativeLibrary()
   at System.Drawing.SafeNativeMethods.Gdip..cctor()
   --- End of inner exception stack trace ---
   at System.Drawing.SafeNativeMethods.Gdip.GdipCreateBitmapFromFile(String filename, IntPtr& bitmap)
   at System.Drawing.Bitmap..ctor(String filename, Boolean useIcm)
   at gdiplus.Program.Main(String[] args) in /root/gdiplus/Program.cs:line 12

This is because by default libdl.so doesn’t exist on CentOS whereas libdl.so.2 does exist.

As a workaround you can symlink /lib64/libdl.so.2 to /lib64/libdl.so.

ln -s /lib64/libdl.so.2 /lib64/libdl.so

Program.cs:

using System;
using System.Drawing;

namespace gdiplus
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var bitmap = new Bitmap("test.bmp");
        }
    }
}

Project file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="System.Drawing.Common" Version="4.5.0-preview1-25718-03"/>
  </ItemGroup>
</Project>

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 2
  • Comments: 33 (18 by maintainers)

Commits related to this issue

Most upvoted comments

@henkmollema In the meanwhile, if you’re on Ubuntu or Debian, you can try to install the libc6-dev package to get libdl.so.

in my case of Docker container with base image microsoft/aspnetcore:2.0.5 after running these commands the error disappear

$ ln -s /lib/x86_64-linux-gnu/libdl.so.2 /lib/x86_64-linux-gnu/libdl.so
$ apt update
$ apt install libgdiplus
$ ln -s /usr/lib/libgdiplus.so /lib/x86_64-linux-gnu/libgdiplus.so

Fixed in .NET Core 3.0