从GtkDrawingArea获取坐标的像素颜色

时间:2011-08-05 05:35:44

标签: c gtk cairo

这个问题几乎总结了我的问题。我有一个GtkDrawingArea,它的表面(cairo_surface_t)格式为CAIRO_FORMAT_INVALID(默认),即该表面的数据“不存在或支持这种格式”。

有没有办法用我选择的任何格式创建GtkDrawingArea曲面的副本?然后我就能知道它的格式来查询数据。

1 个答案:

答案 0 :(得分:0)

由于目标是获得彩色像素,以下内容对我有用:

typedef struct{
 guint16 red;
 guint16 green;
 guint16 blue;
 guint16 alpha;
} color_struct;

color_struct get_pixel_pixbuf(double x,double y,GdkPixbuf *pixbuf,unsigned char *pixels){
  color_struct color;
  guchar *p;
  p = pixels + ((int)y) * gdk_pixbuf_get_rowstride (pixbuf) + ((int)x) * gdk_pixbuf_get_n_channels(pixbuf);

  color.red = p[0];
  color.green = p[1];
  color.blue = p[2];
  color.alpha = p[3];

  return color;
}

GtkWidget *drawingarea;
GdkPixbuf *surface_pixbuf = gdk_pixbuf_get_from_drawable(NULL,GDK_DRAWABLE(drawingarea->window),gdk_colormap_get_system(),0,0,0,0,drawingarea->allocation.width,drawingarea->allocation.height);
pixbuf_pixels = gdk_pixbuf_get_pixels (surface_pixbuf);


color_struct *pixel_color = get_pixel_pixbuf(someX,someY,surface_pixbuf,pixbuf_pixels);

由于绘图区域的类型是xlib(不是图像表面),因此无需创建后一种类型的副本。

相关问题