尝试将 GTK+ 窗口呈现为图像

Trying to render GTK+ window to image

本文关键字:图像 窗口 GTK+      更新时间:2023-10-16

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 的情况下运行,所以生成的图像是空的。