如何从 Cairo 上下文创建 GtkImage?
我得到了一个使用 Cairo 上下文工作的绘制函数,最终结果应该是一个 GtkImage (没有中间图像创建)。我尝试使用 gdk_cairo_create 函数,但此代码:
...
GdkPixbuf *pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 22, 22);
GtkWidget *image = gtk_image_new_from_pixbuf (pixbuf);
GdkDrawable *drawable = image->window;
cairo_t *ctx = gdk_cairo_create (drawable);
my_cairo_paint_function (ctx);
...
失败并显示:
Gdk-CRITICAL **:IA__gdk_cairo_create:断言“GDK_IS_DRAWABLE(可绘制)”失败
与简单相同:
#include <gtk/gtk.h>
#include <cairo.h>
int
main (int argc, char *argv[])
{
gtk_init(&argc, &argv);
cairo_t *ctx = gdk_cairo_create (gtk_widget_get_window (gtk_image_new_from_file ("foobar.png")));
gtk_main();
return 0;
}
我不明白为什么会失败。任何帮助表示赞赏!
I got a paint function that works using a Cairo context and the end result should be a GtkImage (without intermediate image creation). I tried to use the gdk_cairo_create function but this code:
...
GdkPixbuf *pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 22, 22);
GtkWidget *image = gtk_image_new_from_pixbuf (pixbuf);
GdkDrawable *drawable = image->window;
cairo_t *ctx = gdk_cairo_create (drawable);
my_cairo_paint_function (ctx);
...
fails with:
Gdk-CRITICAL **: IA__gdk_cairo_create: assertion `GDK_IS_DRAWABLE (drawable)' failed
Same with a simple:
#include <gtk/gtk.h>
#include <cairo.h>
int
main (int argc, char *argv[])
{
gtk_init(&argc, &argv);
cairo_t *ctx = gdk_cairo_create (gtk_widget_get_window (gtk_image_new_from_file ("foobar.png")));
gtk_main();
return 0;
}
I don't understand why this fails. Any help is appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
GtkImage
没有GdkWindow
,因此调用gtk_widget_get_window()
或访问widget->window
code> 返回NULL
。您可以将GtkImage
放入GtkEventBox
中,并在事件框的GdkWindow
上进行绘制。尽管如此,看起来您正在尝试(使用
gdk_pixbuf_new
)创建一个空白空间来绘制。在这种情况下,GtkImage
不是您想要的小部件 - 请使用GtkDrawingArea
。并且不要忘记在event-expose
信号的处理程序中调用您的绘制函数!GtkImage
doesn't have aGdkWindow
, so the call togtk_widget_get_window()
or the access ofwidget->window
returnsNULL
. You can put theGtkImage
in aGtkEventBox
and draw on the event box'sGdkWindow
.Although, it looks like you're trying (with
gdk_pixbuf_new
) to create an empty space to draw on. In that case,GtkImage
is not the widget you want -- useGtkDrawingArea
. And don't forget to call your paint function in the handler for theevent-expose
signal!