Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image.FromFile() or FromStream() hangs on GdiplusStartup

I have a simple C# Application containing this line:

var mImage = System.Drawing.Image.FromFile(filename);

When running this code on Windows (.NET), the image is loaded correctly. When running the same code on OS X (Mono), the application just hangs. The debugger stays in that call forever. No exception no nothing.

The callstack shows the application hangs at:

System.Drawing.GDIPlus.GdiplusStartup ()

What could go wrong here?

PS: I have the latest versions of Xamarin Studio and Mono installed.

like image 812
Boris Avatar asked Nov 12 '14 18:11

Boris


2 Answers

1) Not only check you have the latest Xamarin Studio version but check you have the latest build!!!

Bug report:

This problem was reported by Troy Dawson using Xamarin Studio Version 5.5 (build 198), just over a month ago: https://bugzilla.xamarin.com/show_bug.cgi?id=23444

"With the latest Mono from the alpha release channel, attached project should raise an typenotfound exception when run at:

var image = Image.FromStream(content);

Because GDIPlus can't be initialized or something.

This is similar to another bug I filed where Image.FromStream() takes a very long time to return when Mono is first run."

Full exception:

An exception was thrown by the type initializer for System.Drawing.GDIPlus ---> System.Exception: GdiplusStartup

Solution:

A day after the bug was reported on 2014-09-29 Sadik Ali confirms he can reproduce the problem Comment 1, then 4 days later Troy Dawson confirms that Xamarin Studio Version 5.5 (build 227) fixes the issue Comment 2.


2) If it still fails with the latest build I suggest you go through the limitations and make sure the image(s) you're loading are supported: https://github.com/mono/libgdiplus/blob/master/TODO
like image 110
Jeremy Thompson Avatar answered Oct 22 '22 20:10

Jeremy Thompson


The problem is MonoMac GDI's way of interfacing with Mac fonts. The first time it is launched, it builds a font-cache it needs, and this actually takes a few minutes. Just let it run for a while. This delay goes away once the font cache is built.

If you kill it, it never finishes building the cache, and it'll happen every time. It's a shame they don't even yet have any kind of dialog or message printed that this is happening.

In rare situations, your font-cache directory may not be writable by mono. In this case, you can either run the app as root once, or you can follow the instructions here.

like image 38
David Jeske Avatar answered Oct 22 '22 21:10

David Jeske