【发布时间】:2020-12-12 06:50:26
【问题描述】:
我正在寻找从 GtkDrawingArea 获取 pixbuf 的最佳方式。 我看到有两种方法可以做到这一点。
- 首先我可以使用
gdk_pixbuf_get_from_surface。这样做可能很容易,但我只是将图片映射到 GtkDrawingArea 中,而不是用 cairo 制作的画。 - 第二,
gdk_pixbuf_get_from_window。这是我选择的方式,因为我认为它符合我的需求。确实,我想要的只是对我的绘图区域进行快速快照,并在图片上绘制所有标签和绘画。
我的以下代码在 GNU/Linux 上按预期工作,但在给我黑色 png 的 Windows 上。有人可以帮我解决这个问题吗?
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);
}
}
【问题讨论】: