Runtime: Improve GDIP exception when failing to load `libgdiplus` or GDI+

Created on 29 Apr 2019  路  18Comments  路  Source: dotnet/runtime

Hi ,
Someone please help 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

C# code getting the icon.
23

I' ve always got this error message response. when accessing the API endpoint in Ubuntu Server.
54

Someone can help to fix this error.
Thank you.

area-System.Drawing up-for-grabs

Most helpful comment

Accepted!

All 18 comments

GDI+ is Windows only tech.
ImageSharp is cross-platform.

On Unix, we use libgdi+ for GDI+ so I would think this should work. I would start by investigating this as a possible failure to load the shared library. Set LD_DEBUG and go from there.

Longer term you might consider other libraries, since System.Drawing is not expected to get much investment - it is very old.

GDI+ is Windows only tech.
ImageSharp is cross-platform.

Thank you.
I've installed "libc6-dev" in my linux server and it works.

@jlestercastro ideally that message would be more helpful. @safern would it make sense to wrap the Gdip() static constructor in a try/catch to add some more useful information, such as to make sure libgdiplus version XYZ or later is available, and possibly to try LD_DEBUG?

If we load by the runtime API, it will show this message today, and I know it has helped some people:

  507:     IDS_EE_NDIRECT_LOADLIB_LINUX            "Unable to load shared library '%1' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: %2"

Yes it would make sense. We could potentially just wrap this call: https://github.com/dotnet/corefx/blob/dee434428ee392302ff2050bbee304c9fce68b2f/src/System.Drawing.Common/src/System/Drawing/Gdiplus.cs#L77

I will rename the title and mark as up for grabs and 3.0.

I'd like to tackle this one.

@eanova sure - it's yours. Thanks

@eanova let me know if you need any help on this workitem.

@karelz I can't assign the issue to @eanova eventhough is a Contributor. Could you help assigning?

I sent @eanova collaborator invite - that will make it work. Please ping us here once you accept.
Also, it will auto-subscribe you to all notifications from the repo (500+ per day). We recommend to switch the repo to Not Watching -- which will give you notifications for all your mentions, assignments and explicit subscriptions to issues.

Accepted!

@jlestercastro I re-named the issue to improve the exception message and I believe @eanova is working on it, so I will re-open the issue to track that work.

Hi everyone! I've been chipping away at this and trying to get comfortable tooling at the same time. I found this article that discusses Mono debugging of libgdiplus using LD_DEBUG helpful.
https://www.mono-project.com/docs/gui/problemgdiplusinit/

I guess there really isn't more information we can capture such as individual dependencies that are missing. So I'll pretty much stick to what @danmosemsft has already suggested

I'll test the situation on the Mac, and then send a PR.

Moving to Future. Work can still happen, but it's not essential that we fix this before ship 3.0.

Is there an effective way to debug a local build of System.Drawing.Common. I've tried the method described in the dogfooding article but Nuget can't seem to find the nupkg. Or is there away to see the debug output within the xUnit console?

Is there an effective way to debug a local build of System.Drawing.Common

Does running the CoreFX test's doesn't help?

If you want to use a local build of corefx rather than running the tests you can do the following:
run build -allConfigurations in your corefx repo. That will produce some packages in your local repository under artifacts\packages\<BuildConfiguration> which in this case <BuildConfiguration> will be Debug. You will find a System.Drawing.Common package with a 4.6.0-dev version.

Once you have that, you can add a NuGet.config with nuget.org and your local folder as restore sources. Also, you can directly add the restore sources on your csproj like this:

<PropertyGroup>
    <RestoreSources>
         https://api.nuget.org/v3/index.json;
         <PathToCorefx>\artifacts\packages\Debug\;
         $(RestoreSources) 
   </RestoreSources>
</PropertyGroup>

@safern Thanks, I was finally able to get the -allconfigurations flag to work. I'll be pushing some simple fixes that I needed to get it to work. Adding the restore to the project file is working as well.

Thank you everyone

Was this page helpful?
0 / 5 - 0 ratings

Related issues

GitAntoinee picture GitAntoinee  路  3Comments

sahithreddyk picture sahithreddyk  路  3Comments

aggieben picture aggieben  路  3Comments

yahorsi picture yahorsi  路  3Comments

Timovzl picture Timovzl  路  3Comments