如何抓取屏幕外 NSWindow 的图像?

发布于 2024-09-29 10:45:00 字数 827 浏览 2 评论 0原文

我正在尝试获取屏幕外窗口的图像以在基于 CALayer 的动画中使用,但无论我尝试哪种方法,我都无法从另一侧获取图像。这是我在项目中的自定义 NSWindow 子类上使用的代码:

CGImageRef windowImage = CGWindowListCreateImage(CGRectNull, 
                                                 kCGWindowListOptionAll, 
                                                 (CGWindowID)[self windowNumber], 
                                                 kCGWindowImageDefault);

图像应如下所示:

alt text

这是我得到的结果(忽略稍大的尺寸 - 这是我在图像渲染中的错误):

alt text

谁能看出我哪里出错了?奇怪的是,Quartz Debug 可以毫无问题地看到窗口图像,但是 Apple 的 Grab 示例代码之子 不能。

I'm trying to get an image of an offscreen window to use in a CALayer-based animation, but no matter which method I try I cannot get an image out the other side. Here is the code I've been using on a custom NSWindow subclass in my project:

CGImageRef windowImage = CGWindowListCreateImage(CGRectNull, 
                                                 kCGWindowListOptionAll, 
                                                 (CGWindowID)[self windowNumber], 
                                                 kCGWindowImageDefault);

This is what the image should look like:

alt text

and here's what I get (ignore the slightly larger size - that's my fault in the rendering of the image):

alt text

Can anyone see where I'm going wrong? Strangely, Quartz Debug can see the window image without issue, but the Son of Grab sample code from Apple cannot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷默言语 2024-10-06 10:45:00

来自 CGWindow.h 中 CGWindowListCreateImage 的文档注释:

kCGWindowListOptionAllkCGWindowListOptionOnScreenOnly:使用此用户会话中的所有屏幕窗口来构造图像。参数windowID应为kCGNullWindowID

这就是为什么您要获取您感兴趣的矩形内的所有窗口的屏幕截图。

要获取特定窗口的屏幕截图,请使用kCGWindowListOptionInclusionWindow

From the documentation comment for CGWindowListCreateImage in CGWindow.h:

kCGWindowListOptionAll, kCGWindowListOptionOnScreenOnly: Use all on-screen windows in this user session to construct the image. The parameter windowID should be kCGNullWindowID.

That's why you're getting a screenshot of all the windows that are within the rectangle you're interested in.

To take a screenshot of a specific window, use kCGWindowListOptionIncludingWindow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文