I have a C# / Mono / Gtk# program that simply loads files dropped in the window as Gdk.Pixbuf and displays them.
It works well on Ubuntu. But on Windows if I try to drop a file with a non-ASCII filename such as C:\áéíóú.jpg, the program will crash. I thought it was a problem with my code first. So I did a simple testcase.
Console.WriteLine("{0} exists? {1}", Filename, File.Exists(Filename));
Pixbuf pixels = new Pixbuf (Filename)
Output
C:\áéíóú.jpg exists? True
GLib.GException: Failed to open file ‘C:\áéíóú.jpg’: No such file or directory
As it turns out, Glib can’t figure out a file that exists, exists. And I don’t know how to fix it so that I can load an image file into Pixbuf on windows from a Unicode filename.
This seems to be a problem in the gtk-sharp<->gdk-pixbuf interoperation.
Apparently on every OS except windows,
gdk_pixbuf_new_from_filetakes file name encoded in utf8. On windows, however, this function is renamed togdk_pixbuf_new_from_file_utf8and replaced by a wrapper that does locale conversion and proceeds to call the utf8 version. gtk-sharp doesn’t know this and usesgdk_pixbuf_new_from_filepassing utf8 argument, hence the unexpected extra locale conversion on windows breaks the filename.As a workaround I have suggested to use the
Pixbufconstructor that takes aStreaminstead of a filename, but the poster has reported that doesn’t load his image properly.Update:
Luckily the Pixbuf wrapper class has a constructor that accepts the raw IntPtr of an existing pixbuf object. As such, the code in the buggy constructor can be duplicated, fixed and hidden in some helper methods, such as:
I have tested this with success. Hope this helps.