本文介绍了如何获得Gtk窗口的PNG(alpha通道)截图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Gtk窗口使用开罗支持透明度。我试着用调用。该文档明确地说:

所以我认为将alpha通道作为第一个参数传递给pixbuf应该足够了。



另一种方法是使用cairo API,它可以与GTK 2和GTK 3一起使用。


Gtk windows support transparency using Cairo. I tried to get a screenshot with the code here. But it prints pure black for windows that use transparency. How can I make a screenshot (PNG) which caputures the alpha channel too?

EDIT: I tried with alpha pixbuf, but the output is still with no alpha. Here's my code (sing Gtk#, but it's pretty identical to C):

static void GetScreenshot(Gtk.Window win) {
    var pixbuf = new Gdk.Pixbuf(Gdk.Colorspace.Rgb, true, 8, 500, 500);
    var pix = pixbuf.GetFromDrawable(win.GdkWindow, win.Colormap, 0, 0, 0, 0, 500, 500);

    pix.Save("/home/blez/Desktop/screenshot.png", "png");
}

Here's the output:

EDIT: I did it with Cairo surface. Here's the code I used:

static void GetScreenshot(Gtk.Window win) {
    var src_context = Gdk.CairoHelper.Create(win.GdkWindow);
    var src_surface = src_context.Target;
    var dst_surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, win.Allocation.Width, win.Allocation.Height);
    var dst_context = new Cairo.Context(dst_surface);
    dst_context.SetSourceSurface(src_surface, 0, 0);
    dst_context.Paint();
    dst_surface.WriteToPng("screenshot.png");
}
解决方案

In your example, the answer lies on the gdk_pixbuf_get_from_drawable call. The documentation explicitely says:

So I think that passing a pixbuf with an alpha channel as the first argument should be enough.

Another way to do it is to use the cairo API, which would work with GTK 2 and GTK 3.

这篇关于如何获得Gtk窗口的PNG(alpha通道)截图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 16:18
查看更多