C GTK+ : Copying an image over another one
Articles may may have files attached at the end of the post
Submitted by chantra on Sat, 12/12/2009 - 17:25
GTK+ offers a nice toolkit through the GDK library to make image manipulation quite easy.
This page will go through the process of modifying an image but putting another on on top as an overlay. In this example, we will be copying an MSN icon on top of a "busy" icon.
Another use case could be to watermark images.
Here is the bit of code used in this example, and the result:
GTK image overlay
The original file is available in a .tar.gz at the bottom of this article
- /**
- * compile with:
- * gcc -Wall -Werror -g -o -DGDK_PIXBUF_DISABLE_DEPRECATED -DGDK_DISABLE_DEPRECATED -DGTK_DISABLE_DEPRECATED -o gtkoverlay gtkoverlay.c `pkg-config --cflags gtk+-2.0 --libs`
- * Available under GPLv2
- */
- #define PT_MSN "images/im-msn.svg"
- #define ST_BUSY "images/empathy-busy.svg"
- #include <gtk/gtk.h>
- int
- main (int argc, char **argv)
- {
- GtkWidget *window;
- GtkWidget *img; /* result img */
- GdkPixbuf *pixb1, *pixb2; /* destination, source */
- int ls, ss; /* large size, small size */
- gtk_init (&argc, &argv);
- window = img = NULL;
- ls = 32;
- ss = 24;
- pixb1 = gdk_pixbuf_new_from_file_at_size (ST_BUSY, ls, ls, NULL);
- pixb2 = gdk_pixbuf_new_from_file_at_size (PT_MSN, ss, ss, NULL);
- if( !pixb1 || !pixb2 ){
- fprintf (stderr, "Error loading one of the image!\n");
- if( pixb1 ) g_object_unref( G_OBJECT (pixb1) );
- else g_object_unref( G_OBJECT (pixb2) );
- return 1;
- }
- gdk_pixbuf_composite( pixb2, pixb1, /* src, dst */
- 0, ls - ss, /* destx, desty */
- ss, ss, /* destwdth, destheight */
- 0, ls - ss, /* offset_x, offset_y */
- 1, 1, /* scale_x, scale_y */
- GDK_INTERP_BILINEAR, 255 ); /* interp_type, overall_alpha */
- img = gtk_image_new_from_pixbuf (pixb1);
- g_object_unref( G_OBJECT (pixb1) );
- g_object_unref( G_OBJECT (pixb2) );
- window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
- gtk_container_add( GTK_CONTAINER (window), img );
- gtk_widget_show_all( window );
- gtk_main();
- return 0;
- }
The tricky part is with gdk_pixbuf_composite which is best explained on the api itself.
| Attachment | Size |
|---|---|
| gtk-img-overlay.tar_.gz | 5.24 KB |
Similar entries
- C GTK+ : Text Completion on a GtkEntry with GtkEntryCompletion
- Python GTK: How to set up gtk.Notebook tab with custom widget
- Python GTK: How to customize the size of a button in a notebook tab label
- Python GTK: How to set up gtk.Notebook tab with custom widget -- page 2
- Python: Embedding a Virtual Terminal in a GTK widget with python vte library












