How to get a PNG (alpha channel) screenshot of a Gtk window?

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 take a screenshot (PNG), which also calls the alpha channel?

EDIT: I tried with alpha pixbuf, but the output is still no alpha. Here is my code (sing Gtk #, but it is very similar 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 conclusion: enter image description here

EDIT: I did it with the surface of Cairo. Here is 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"); } 
+1
source share
2 answers

In your example, the answer lies in the call to gdk_pixbuf_get_from_drawable . The documentation states:

If the specified pixbuf dest destination is NULL, then this function will create an RGB-pixbuf with 8 bits per channel and there will be no alpha with the same size specified by the width and height arguments. In this case, the arguments dest_x and dest_y should be specified as 0. If the specified pixbuf destination address is not NULL and contains alpha information, then the filled pixels will be set to full opacity (alpha = 255) .

So, I think it’s enough to pass pixbuf with the alpha channel as the first argument.

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

+2
source

... and how do you want the alpha channel in

 var pixbuf = new Gdk.Pixbuf(Gdk.Colorspace.Rgb, true, 8, 500, 500); ? 

If it has transparency (real), then rgba. But you can still have problems with the window manager, if it is not a composite, but imitates it by mixing images, returns black (0) instead of the color / alpha value.

0
source

Source: https://habr.com/ru/post/897848/


All Articles