使用 GTK3 获取 GtkDrawingArea 的快照,WIndows 上的错误?
get a snapshot of GtkDrawingArea with GTK3, bug on WIndows?
我正在寻找从 GtkDrawingArea 获取 pixbuf 的最佳方法。
我看到有两种方法可以做到这一点。
- 首先我可以使用
gdk_pixbuf_get_from_surface
。这可能很容易,但我只是将图片映射到 GtkDrawingArea 中,而不是用 cairo 制作的绘画。
- 其次,
gdk_pixbuf_get_from_window
。这是我选择的方式,因为我认为它符合我的需要。事实上,我想要的只是快速快照我的绘图区域,其中包含所有标签和在图片上绘制的绘画。
我的以下代码在 GNU/Linux 上按预期工作,但在 Windows 上给我黑色 png。有人可以帮我解决这个问题吗?
gtk_widget_get_allocation(widget, &allocation_w);
GdkWindow *window = gtk_widget_get_parent_window(widget);
if (window) {
pixbuf = gdk_pixbuf_get_from_window(window, allocation_w.x, allocation_w.y, allocation_w.width, allocation_w.height);
if (pixbuf) {
file = g_file_new_build_filename(com.wd, filename, NULL);
g_free(filename);
stream = (GOutputStream*) g_file_replace(file, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &error);
if (stream == NULL) {
if (error != NULL) {
g_warning("%s\n", error->message);
g_clear_error(&error);
}
g_object_unref(pixbuf);
g_object_unref(file);
return;
}
gdk_pixbuf_save_to_stream_async(pixbuf, stream, "png", NULL,
snapshot_callback, (gpointer) g_file_get_basename(file), NULL);
g_object_unref(stream);
g_object_unref(pixbuf);
g_object_unref(file);
}
}
我现在有了答案。
从 window 获取 pixbuf 的 API 几乎是 GTK 仅用于 X11 时的遗留物。所以不要指望让它在 GNU/Linux X11 环境之外工作。该功能也已从 GTK4 API 中删除。
所以,最好的方法是使用 gdk_pixbuf_get_from_surface
并重绘我需要的东西。
我想我也可以使用 GtkOffscreenWindow 但我没有资助任何使用示例。
我正在寻找从 GtkDrawingArea 获取 pixbuf 的最佳方法。 我看到有两种方法可以做到这一点。
- 首先我可以使用
gdk_pixbuf_get_from_surface
。这可能很容易,但我只是将图片映射到 GtkDrawingArea 中,而不是用 cairo 制作的绘画。 - 其次,
gdk_pixbuf_get_from_window
。这是我选择的方式,因为我认为它符合我的需要。事实上,我想要的只是快速快照我的绘图区域,其中包含所有标签和在图片上绘制的绘画。
我的以下代码在 GNU/Linux 上按预期工作,但在 Windows 上给我黑色 png。有人可以帮我解决这个问题吗?
gtk_widget_get_allocation(widget, &allocation_w);
GdkWindow *window = gtk_widget_get_parent_window(widget);
if (window) {
pixbuf = gdk_pixbuf_get_from_window(window, allocation_w.x, allocation_w.y, allocation_w.width, allocation_w.height);
if (pixbuf) {
file = g_file_new_build_filename(com.wd, filename, NULL);
g_free(filename);
stream = (GOutputStream*) g_file_replace(file, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &error);
if (stream == NULL) {
if (error != NULL) {
g_warning("%s\n", error->message);
g_clear_error(&error);
}
g_object_unref(pixbuf);
g_object_unref(file);
return;
}
gdk_pixbuf_save_to_stream_async(pixbuf, stream, "png", NULL,
snapshot_callback, (gpointer) g_file_get_basename(file), NULL);
g_object_unref(stream);
g_object_unref(pixbuf);
g_object_unref(file);
}
}
我现在有了答案。
从 window 获取 pixbuf 的 API 几乎是 GTK 仅用于 X11 时的遗留物。所以不要指望让它在 GNU/Linux X11 环境之外工作。该功能也已从 GTK4 API 中删除。
所以,最好的方法是使用 gdk_pixbuf_get_from_surface
并重绘我需要的东西。
我想我也可以使用 GtkOffscreenWindow 但我没有资助任何使用示例。