C# Mono {new Bitmap(“fileName”);} Just hangs on OS

2020-07-08 06:52发布

问题:

When trying to load either a ".bmp", ".png" or ".jpg" on OSX 10.7.3 using mono's version of the System.Drawing.Bitmap object the applications just hangs. I get no error, the app just is stuck on the Bitmaps constructor...

When I run the same code on (Arch)Linux or Windows everything works fine.

public static void Main (string[] args)
{
    using (var bitmap = new Bitmap("/....../image.bmp"))
    {

    }

    Console.WriteLine ("Hello World!");// Never gets here...
}

If I pause the application in debug mode it opens the "disassembly" window and shows its stuck on this line:

call Status System.Drawing.GDIPlus:GdiplusStartup (UInt64, GdiplusStartupInput, GdiplusStartupOutput)

NOTE: After pausing the application in debug mode a couple of times it "Magically" started to work while writing this. I promis I did not change any code. Anybody know what can cause "System.Drawing.GDIPlus" to hang so I know how to avoid it?? Is there a mono codex setting file or something that could have bin messed up?

回答1:

The problem is just performance. It has nothing to do with you code; the image is just taking a longer time to load.
Mono's implementation of System.Drawing is just a C# wrapper and is kinda incomplete and does not (or incorrectly) provide all features in System.Drawing. It may function well in windows because it uses the native GDIPlus.dll but in may not function well in unix-based systems because it uses the Cairo engine. You can find more information here.



回答2:

Having the same problem with a new Mono 3.8 install on 10.10; but it went away, suspiciously after I broke during the hanging, seeing thread #4 in GdiplusStartup().

Maybe it's related to the break, or maybe GdiplusStartup just takes a long time on first launch on OS X, doing font caching or something.