Re: Use a .png file



Bob Caryl wrote:
It is a gnome application, so I suppose it is safe to assume that you
must install it to use it.

The reason I use this method is because I was not too impressed by the
quality of the xpm export from gimp, versus the quality of the
conversion of .png files by gnome-pixbuf-csource.  I use this method for
very small icons and all the way up to larger background images.  I have
been quite satisified with the results.

Bob

R. Douglas Barbieri wrote:


On 12/7/05, Bob Caryl <bob fis-cal com> wrote:



Your original question was:

"How can I include a .png or .bmp image in my source code?"

All the answers I have seen so far show you how to display an image from a file at run time.  None of these show how to include the image in your source code.

The following code displays an image that is compiled as data into the executable itself:

Glib::RefPtr<Gdk::Pixbuf> fis_icon = Gdk::Pixbuf::create_from_inline(-1,fisicon_inline,FALSE);

fis_icon is a data structure that is produced by a gnome command line utility called "gdk_pixbuf_csource."  You can use the "man" command to find out about the calling syntax and output options for this utility.  Then you merely take the file it produces and include it in your source code and the image data is compiled into your application.


Wow! That is a cool solution. For me, I open up the image in gimp,
save it out as an XPM, then include the image as source code: #include
"image.xpm". Then I do

Glib::RefPtr<Gdk::Pixbuf> image = Gdk::Pixbuf::create_from_xpm_data(
image_xpm );

So is the gdk_pixbuf_csource function more efficient than xpm
conversion? Also, is it only in gnome?


I've been thinking about methods similar to this myself. I have a program that loads a lot of small .svg files. What I've seen is actually including the image as an unsigned char array. I used Glade to create just a single window with a Gtk::Image in it and I specified the file in the image properties. After that I used Glade to generate the code and it did something like this:

====================================================================================
static const unsigned char icon_svg_data[] =
{       	60,63,120,109,108,32,118,101,114,115,105,111,110,61,34,49,
	46,48,34,32,101,110,99,111,100,105,110,103,61,34,85,84,
	70,45,56,34,32,115,116,97,110,100,97,108,111,110,101,61,
	34,110,111,34,63,62,10,60,33,45,45,32,67,114,101,97,
	116,101,100,32,119,105,116,104,32,73,110,107,115,99,97,112,
	101,32,40,104,116,116,112,58,47,47,119,119,119,46,105,110,
	107,115,99,97,112,101,46,111,114,103,47,41,32,45,45,62
};

Glib::RefPtr<Gdk::PixbufLoader> _image1_loader=Gdk::PixbufLoader::create();
_image1_loader->write(specialokecount_chip_svg_data, sizeof specialokecount_chip_svg_data);
_image1_loader->close();

Gtk::Image *image1 = Gtk::manage(new class Gtk::Image(_image1_loader->get_pixbuf()));
====================================================================================

And if you put a call to set_size() before the write() function you can scale you svg images.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]