GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

GdkPixmap offscreen rendering

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Example Code
Author Message
dreblen
Never Seen the Sunlight


Joined: 14 Jun 2007
Posts: 643
Location: Falun, WI USA

PostPosted: Wed Sep 17, 2008 4:23 pm    Post subject: GdkPixmap offscreen rendering Reply with quote

This is an example of using a GdkPixmap to create an image without requiring a file or using gdk-pixbuf-csource.
This is something that I've needed before and I thought I'd share how it can be done.

You can compile on a unix-like system (or msys or cygwin) with this:
Code: (Plaintext)
1
gcc main.c -o main `pkg-config --cflags --libs gtk+-2.0`


Here's the code, which results in something that looks like a red die:
main.c
Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <gtk/gtk.h>
#include <math.h>

int main(int argc, char **argv)
{
    GtkWidget *win;
    GtkWidget *image;
    cairo_t *cr;
    GdkColormap *cmap;
    GdkPixmap *pmap;
    GdkPixbuf *pbuf;

    gint w, h;

    /* you can set w and h to whatever you want */
   
w = 200;
    h = 200;

    gtk_init(&argc, &argv);

    win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(win, "delete-event", G_CALLBACK(gtk_main_quit), NULL);

    /* get a default GdkColormap */
   
cmap = gdk_colormap_get_system();
    /* create a new GdkPixmap for drawing */
   
pmap = gdk_pixmap_new(NULL, w, h, gdk_colormap_get_visual(cmap)->depth);
    /* create a cairo context from the pixmap */
   
cr = gdk_cairo_create(GDK_DRAWABLE(pmap));

    /* fill the background red */
   
cairo_set_source_rgb(cr, 1, 0, 0);
    cairo_rectangle(cr, 0, 0, w, h);
    cairo_fill(cr);

    /* create a white circle in the center */
   
cairo_set_source_rgb(cr, 1, 1, 1);
    cairo_arc(cr, w/2, h/2, MIN(w/4, h/4), 0, 2*M_PI);
    cairo_fill(cr);

    /* free the cairo context */
   
cairo_destroy(cr);

    /* create a new GdkPixbuf from the pixmap */
   
pbuf = gdk_pixbuf_get_from_drawable(NULL, pmap, cmap, 0, 0, 0, 0, w, h);
    /* create a new GtkImage from the pixbuf */
   
image = gtk_image_new_from_pixbuf(pbuf);
    gtk_container_add(GTK_CONTAINER(win), image);
    gtk_widget_show(image);

    gtk_widget_show(win);

    gtk_main();

    return 0;
}


Here are some links to the functions called in the example:
http://library.gnome.org/devel/gdk/stable/gdk-Colormaps-and-Colors.html#gdk-colormap-get-system
http://library.gnome.org/devel/gdk/stable/gdk-Bitmaps-and-Pixmaps.html#gdk-pixmap-new
http://library.gnome.org/devel/gdk/stable/gdk-Pixbufs.html#gdk-pixbuf-get-from-drawable
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Example Code All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP