[cairo] Regarding saving a image surface as PNG

Chandra Shekhar Sengupta sayan801 at gmail.com
Thu Apr 17 05:54:51 PDT 2008


Hi ,
i have tried with adding "get_target" in all possible places, but still
"write_png" is saving zero byte png.i have attched the full code . Could you
tell how to save that surface?

////////////////////////////////////////////////////////////////////////////////////////
cairo_surface_t *image,*tsurf;
static gdouble rotate_angle = 30.0,scale_factor = 1.5;
GtkWidget* da;


static gboolean
on_expose_event(GtkWidget *widget,
    GdkEventExpose *event,
    gpointer data)
{
  cairo_t *cr;
gint width,height;

cr = gdk_cairo_create (widget->window);

width = cairo_image_surface_get_width(image);
height = cairo_image_surface_get_height(image);

cairo_translate(cr,width/2,height/2);
cairo_scale(cr,scale_factor,scale_factor);
cairo_rotate(cr,rotate_angle*M_PI/180.0);

gtk_widget_set_size_request (da, width*scale_factor,height*scale_factor);

 cairo_set_source_surface(cr, image,-width/2,-height/2);
 cairo_paint(cr);
 tsurf = cairo_get_target(cr);

if(cairo_surface_status(tsurf) != CAIRO_STATUS_SUCCESS)
 g_print(" nil surface \n");
else
 cairo_surface_write_to_png (tsurf, "sticker.png");
  cairo_destroy(cr);

  return FALSE;
}


int main(int argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *fixed,*image1;
  gint width,height;

  image = cairo_image_surface_create_from_png(argv[1]);
  width = cairo_image_surface_get_width(image);
  height = cairo_image_surface_get_height(image);
 g_print("width = %d height = %d \n",width,height);

  gtk_init(&argc, &argv);

window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(window, "destroy",
      G_CALLBACK (gtk_main_quit), NULL);
gtk_window_set_default_size(GTK_WINDOW(window), 800, 480);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);

fixed=gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(window),fixed);

image1=gtk_image_new_from_file("bg.png");
gtk_fixed_put(GTK_FIXED(fixed),image1,0,0);

da = gtk_drawing_area_new ();
gtk_widget_set_size_request (da, width*2,height*2);
gtk_fixed_put(GTK_FIXED(fixed),da,50,50);
g_signal_connect(da, "expose-event",G_CALLBACK (on_expose_event), NULL);

gtk_widget_show_all(window);

gtk_main();

cairo_surface_destroy(image);

  return 0;
}

///////////////////////////////////////////////////////////////////

On Wed, Apr 16, 2008 at 6:32 PM, Gerdus van Zyl <gerdusvanzyl at gmail.com>
wrote:

> You can use get_target anywhere in on_expose, the write_to_png should be
> where it is now, after all painting is done before destroy.
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.cairographics.org/archives/cairo/attachments/20080417/57d522b4/attachment.htm 


More information about the cairo mailing list