How can I convert a System.Drawing.Bitmap
to GDK# Image
so that I can set to the image widget.
I have tried this...
System.Drawing.Bitmap b = new Bitmap (1, 1);
Gdk.Image bmp = new Gdk.Image (b);
UPDATE:
Bitmap bmp=new Bitmap(50,50);
Graphics g=Graphics.FromImage(bmp);
System.Drawing.Font ff= new System.Drawing.Font (System.Drawing.FontFamily.GenericMonospace, 12.0F, FontStyle.Italic, GraphicsUnit.Pixel);
g.DrawString("hello world",ff,Brushes.Red,new PointF(0,0));
MemoryStream ms = new MemoryStream ();
bmp.Save (ms, ImageFormat.Png);
Gdk.Pixbuf pb= new Gdk.Pixbuf (ms);
image1.Pixbuf=pb;
Exception:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> GLib.GException: Unrecognized image file format
at Gdk.PixbufLoader.Close()
at Gdk.PixbufLoader.InitFromStream(Stream stream)
at Gdk.PixbufLoader..ctor(Stream stream)
at Gdk.Pixbuf..ctor(Stream stream)
One ugly, but working, way is to store the bitmap as a PNG in a
MemoryStream
.To save the
Bitmap
, you can use theSave
method:That was easy enough. Loading the PNG data into the Gdk#
Pixbuf
is also rather easy; you can use the appropriate constructor: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 aGtk.Image
is a widget that displays aGdk.Pixbuf
. As such,Gtk.Image
is the equivalent of Windows Forms'PictureBox
, whereasGdk.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:
Bitmap
and before loading yourPixbuf
:ms.Position = 0;
Gtk.Application.Init();
before you do anything withPixbuf
.