So in my recent works with Gtk I’ve come across (and written) some useful snippets so I thought I’d post a few of them.
The first comes from this site (its in Spanish), this snippet allows you to take a System.Drawing.Image to a Gdk.Pixbuf and it looks like this (slightly modified to fix an ambiguous reference in my project).
static Gdk.Pixbuf ImageToPixbuf( System.Drawing.Image image )
{
using ( MemoryStream stream = new MemoryStream() )
{
image.Save( stream, System.Drawing.Imaging.ImageFormat.Png );
stream.Position = 0;
return new Gdk.Pixbuf( stream );
}
}
So, we can take this first function and we can use it in this second snippet which takes an list of images and makes a single pixbuf that contains all of the images. This allows us to take a group of icon files and string them into a single image to be used in a gtk nodeview so that it only requires one column to show a variety of status icons, which is what I’m using it for maybe you can find something more interesting for it to do. Any ways the second snippet I share with you today looks like this.
public Gdk.Pixbuf GenerateIconImage(List<System.Drawing.Image> images)
{
if(images != null && images.Count> 0)
{
Gdk.Pixbuf Composite = ImageToPixbuf(images[0]).Clone() as Gdk.Pixbuf;
images.RemoveAt(0);
foreach (System.Drawing.Image i in images)
{
Composite = CreateComposite(Composite,ImageToPixbuf(i).Clone() as Gdk.Pixbuf);
}
return Composite;
}
else
{
Gdk.Pixbuf Composite = new Gdk.Pixbuf(Gdk.Colorspace.Rgb,true,8,16,16);
Composite.Fill(0);
return Composite;
}
}
public Gdk.Pixbuf CreateComposite(Gdk.Pixbuf p1, Gdk.Pixbuf p2)
{
Gdk.Pixbuf composite = new Gdk.Pixbuf(Gdk.Colorspace.Rgb,true,8,p1.Width+ p2.Width,p1.Height);
p1.Composite(composite,0,0,p1.Width,p1.Height,0,0,1,1,Gdk.InterpType.Hyper,255);
p2.Composite(composite,p1.Width,0,p2.Width,p2.Height,p1.Width,0,1,1,Gdk.InterpType.Hyper,255);
return composite;
}