One ugly, but working, way is to store the bitmap as a PNG in a MemoryStream
.
To save the Bitmap
, you can use the Save
method:
b.Save(myMemoryStream, ImageFormat.Png);
That was easy enough. Loading the PNG data into the Gdk# Pixbuf
is also rather easy; you can use the appropriate constructor:
Pixbuf pb = new Gdk.Pixbuf(myMemoryStream);
You may need to reset the memory stream so the reading position is at the start of the stream before creating the Pixbuf
.
A word of caution: I do not consider this the best, or even a "good" solution. Transferring data between two object-oriented data structures by serializing and deserializing the data has a certain code smell to it. I genuinely hope someone else can come up with a better solution.
EDIT: As for the used libraries: This answer uses only plain GDI+ (System.Drawing.Bitmap
) and Gdk# (Gdk.Pixbuf
). Note that a Gtk.Image
is a widget that displays a Gdk.Pixbuf
. As such, Gtk.Image
is the equivalent of Windows Forms' PictureBox
, whereas Gdk.Pixbuf
is roughly equivalent to Windows Forms' System.Drawing.Bitmap
.
EDIT2: After testing your code, I have found that there are three additional preconditions to ensure before you can run your minimum example:
- As suspected above, you must reset the stream position to the beginning of the after saving your
Bitmap
and before loading yourPixbuf
:ms.Position = 0;
- You must compile the application for x86 CPUs.
- You must invoke
Gtk.Application.Init();
before you do anything withPixbuf
.