Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
610 views
in Technique[技术] by (71.8m points)

save current window as image using gtk#

How can I save the current window as an image file such as a png?

I know I can get a gdkWindow from current windows. I can even get an image. From this image struct I can even query each pixel. But from what I understand what I really need is to get a pixbuf.

int width;
int height;
this.GetSize(width, height);
this.GdkWindow.GetImage(0,0,width,height).?

How do I get a pixbuf from a gtk window or a gdk window or a gdk image ?

EDIT

int width;
int height;
this.GetSize(out width, out height);
Pixbuf pixbuf = Pixbuf.FromDrawable (this.GdkWindow, this.Colormap, 0, 0, 0, 0, width, height);
pixbuf.Save("/tmp/out.png","png");

is kinda working. A png is exported with the correct size but it is blurred.

EDIT 2

The solution is working. The bluriness was a bug of gnome image viewer

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use gdk_pixbuf_get_from_drawable to get from the GdkWindow to a GdkPixbuf, then if you want to save to a file use gdk_pixbuf_save.

In Cairo, which is the more future-proof method, you could use cairo_image_surface_create, pass the result to cairo_create, pass that to gdk_cairo_set_source_window, copy to your image surface with cairo_paint, and write your image with cairo_surface_write_to_png.

Also note that your question is similar to this one. However, since the answer to that question does not work, if my answer answers your question, I'll close the other question as a dupe of this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...