尝试将 GTK+ 窗口呈现为图像
Trying to render GTK+ window to image
GTK使用cairo进行绘图。所以我正在尝试创建一个写入图像(svg、png、...)而不是 X11 的 hello world 应用程序。我面临 2 个问题:- 图像为空- 在没有运行 X11 的情况下启动时(这是实际目标),我收到错误"** (a.out:9021):警告 **:无法打开 X 显示器"
代码是草稿!
#include <string>
#include <iostream>
#include <thread>
#include <chrono>
#include <cmath>
#include <cairo.h>
#include <cairommconfig.h>
#include <cairomm/context.h>
#include <cairomm/surface.h>
#include <gtk/gtk.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
GtkWidget *window;
GtkWidget *button;
// GtkWidget *main_window = gtk_initialize();
window = gtk_offscreen_window_new();
button = gtk_button_new_with_label ("Hello World");
gtk_container_add (GTK_CONTAINER (window), button);
gtk_widget_show (window);
GdkWindow *gdk_window = gtk_widget_get_window(GTK_WIDGET(window));
std::cout << "gdk window: " << gdk_window << std::endl;
cairo_surface_t * surfp = gdk_offscreen_window_get_surface(gdk_window);
std::cout << "Created Window will now draw to png" << std::endl;
std::string filename = "image.svg";
double width = 600;
double height = 400;
Cairo::SvgSurface srfobj(surfp);
Cairo::RefPtr<Cairo::SvgSurface> refptr(&srfobj);
Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create(refptr);
cr->save(); // save the state of the context
cr->show_page();
std::cout << "Wrote SVG file "" << filename << """ << std::endl;
std::chrono::milliseconds dura( 200 );
std::this_thread::sleep_for(dura);
return 0;
}
- 为什么这段代码不起作用?
- 我可以在不运行 X11 的情况下运行 gtk 应用程序,还是应该忽略警告?
这是我基于您的示例代码的解决方案 - 请记住,这是一个肮脏的解决方案,可能无法使用较新版本的 GTK3。它可以保存窗口的UI(仅使用一个按钮进行测试),但仍需要(某处)正在运行的X服务器。它还忽略/不使用您的图片大小设置 - 您必须自己调整大小。我不知道是否(以及如何)也可以剪切这个字符串(X-Server//X-Framebuffer)(DirectFB 似乎不再真正受支持),但是......
玩得愉快!
// Default
#include <string>
#include <iostream>
#include <thread>
#include <chrono>
// cairo / cairomm / gtk
#include <cairo.h>
#include <cairomm/context.h> //libcairomm-1.0-dev
#include <gtk/gtk.h>
int main(int argc, char *argv[]) {
// Init
gtk_init(&argc, &argv);
// Create window with a button
GtkWidget *window;
GtkWidget *button;
window = gtk_offscreen_window_new();
button = gtk_button_new_with_label("Hello World");
gtk_container_add(GTK_CONTAINER(window), button);
gtk_widget_show_all(window);
// Make a gdk window out of it, prepare cairo and draw it to it
GdkWindow* gdk_window = gtk_widget_get_window(GTK_WIDGET(window));
cairo_surface_t* surfp = gdk_offscreen_window_get_surface(gdk_window);
cairo_t* context = cairo_create(surfp);
gtk_widget_draw(GTK_WIDGET(window), context);
// Yay - begin the dump!
Cairo::SvgSurface srfobj(surfp);
std::string filename = "image.png";
srfobj.write_to_png(filename);
std::cout << "Done." << std::endl;
// Aaand a little sleep...
std::chrono::milliseconds dura(1000);
std::this_thread::sleep_for(dura);
return 0;
}
尝试使用 gtk_widget_draw (widget_ptr, cairo_ctx_ptr);
将小部件(或小部件的层次结构)绘制到cario上下文?
这两个问题的答案是,如果没有某种输出,您将无法运行 GTK+ 应用程序。您正在使用需要XServer的gtk-x11。你可能对DirectFB后端有一些运气,但我不会屏住呼吸,因为我不知道它是否已经维护了。
因为 Gtk 不能在没有 XServer 的情况下运行,所以生成的图像是空的。
相关文章:
- 如果不在窗口 10 中声明名为 openCV 的 openCV namedWindow,QT 图像显示将无法正常工作
- C++ 窗口显示无框图像
- 在qt窗口中具有图像,而无需将图像文件本身作为源
- 如何设置与其背景图像大小相对应的窗口大小?
- 如何在 direct2d 中使用随其窗口重新缩放的效果绘制图像
- 创建位图图像未在窗口上打印
- 如何将图像数据从搜索窗口中移动到本地存储器OpenCL
- 在使用 resizeEvent 调整窗口大小时调整 Qlabel 图像的大小
- 如何将子窗口(MFC 应用程序)的工作区保存到图像
- 在没有任何外部库的情况下,是否可以在c++/linux的窗口中显示bmp图像
- 输入层类型:图像数据在窗口咖啡cpp中给出空白输出
- 在调整大小窗口时,如何使用鼠标单击来获取图像的坐标
- 如何在C 和QT中的另一个窗口中显示MainWindow的结果图像
- 在Qt窗口中的一小部分中查看图像
- 如何保持我在 Visual Studio SDL 中编写的图像窗口处于打开状态
- 滚动条缩略图跟踪长度 基于图像的水平适合调整窗口大小时出现问题
- 如何在窗口 MFC 应用程序中更改图像
- Opencv图像窗口/imshow
- 在程序运行期间,图像窗口没有响应openv
- Visual Studio with OpenCV:即使程序退出,我也能保持图像窗口打开吗