5

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.

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
Boris
  • 8,551
  • 25
  • 67
  • 120
  • Is this the standard .NET `Image`? – Erwin Nov 12 '14 at 18:44
  • Yes, it is. I have just updated the question. – Boris Nov 12 '14 at 18:48
  • 1
    Is it just this one image file that causes the problem, or do you get the same hang regardless of which file you try to open? Does the image get loaded as expected (i.e. looks correct, no artifacts) when you load it on Windows? – Peter Duniho Nov 12 '14 at 19:14
  • At least work down the google hits from "osx gdiplusstartup hang". – Hans Passant Nov 12 '14 at 19:14
  • 2
    Guess what I did before asking this question. I could not find a solution to my problem there. – Boris Nov 13 '14 at 06:47
  • 1
    The top result of that search, at this time, is [this other question on StackOverflow](http://stackoverflow.com/questions/9423560/c-sharp-mono-new-bitmapfilename-just-hangs-on-osx), which suggests it doesn't actually hang, it just takes a really long time. How have you determined that "forever" is really "forever"? –  Nov 15 '14 at 18:26
  • 2
    Well, I waited at least 2 minutes. That's pretty much unacceptable (even if it's not "forever"). – Boris Nov 15 '14 at 18:45
  • how large is the image? – Ed S. Nov 15 '14 at 19:59
  • Can you provide the image you use? If not, can you find another, public, image that exhibits the problem? – Jester Nov 17 '14 at 16:31
  • I tried with several images. Mostly small PNGs or JPEGs (< 100kb). – Boris Nov 17 '14 at 16:59

3 Answers3

5

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
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
3

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.

David Jeske
  • 2,306
  • 24
  • 29
1

The problem is probably performance. You code should run fine; the image is just taking a longer time to load, this is due to mono's System.Drawing being just a C# wrapper

Sadly mono's System.Drawing has major issues providing all features in System.Drawing. Windows works well because it uses the native GDIPlus.dll but you code may not work well in unix-based systems.

See more info about mono's System.Drawing here

Depending on how you are using it, there are better alternatives to System.Drawing.

MonoMac suggests Mono.Cairo

This StackOverFlow post may be useful as well

Community
  • 1
  • 1
Burdock
  • 1,085
  • 1
  • 9
  • 22
  • 2
    But, really; two minutes to load what I assume is a small image (perhaps I shouldn't assume that)? That's not just slow, it's broken. – Ed S. Nov 15 '14 at 20:00
  • 1
    Give some of the alternatives I linked a try, and see if that improves things. This would not be the first time System.Drawing didn't work right on mono – Burdock Nov 15 '14 at 20:03