如何使用 Cocoa 或 C++ 在 Mac OS X 中截取屏幕截图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:23:46  来源:igfitidea点击:

How to take screenshot in Mac OS X using Cocoa or C++

c++cocoamacos

提问by pirho

How to take screenshot programmically of desktop area in Mac OS X ?

如何在 Mac OS X 中以编程方式截取桌面区域的屏幕截图?

回答by Peter

Two interesting options I have seen, but yet to use professionally, are the screencapture utility and a MacFuse demo.

我见过但尚未专业使用的两个有趣选项是屏幕捕获实用程序和 MacFuse 演示。

The screencapture utility has been around since 10.2, according to the man page, and could be linked to a Cocoa application by use of NSTask.

根据手册页,screencapture 实用程序从 10.2 开始就已经存在,并且可以通过使用 NSTask 链接到 Cocoa 应用程序。

The MacFuse demo worked by creating a new screenshot each time a folder was opened, or something like that. The idea being you could write a quick script to access the image when you needed it, without having to have the script actually run on that machine.

MacFuse 演示的工作原理是每次打开文件夹时都会创建一个新的屏幕截图,或者类似的东西。这个想法是您可以编写一个快速脚本来在需要时访问图像,而不必让脚本实际在该机器上运行。

But seriously, Apple has some other sample code called "Son of Grab" which uses the new CGWindow API which is pretty awesome.

但是说真的,Apple 还有一些其他的示例代码,称为“Son of Grab”,它使用了非常棒的新 CGWindow API。

http://developer.apple.com/samplecode/SonOfGrab/

http://developer.apple.com/samplecode/SonOfGrab/

回答by Peter

One way of going about doing this would be to use NSTask in conjuction with the 'screencapture' command line command.

进行此操作的一种方法是将 NSTask 与“screencapture”命令行命令结合使用。

For example:

例如:

NSTask *theProcess;
theProcess = [[NSTask alloc] init];

[theProcess setLaunchPath:@"/usr/sbin/screencapture"];
// use arguments to set save location
[theProcess setArguments:@"blahblah"];
[theProcess launch];

The you could open up the file wherever you told it to be saved, process it, and then delete it as needed. Obviously stopgap, but it would work.

你可以在任何你告诉它保存的地方打开文件,处理它,然后根据需要删除它。显然是权宜之计,但它会奏效。

回答by Peter

If you're fine with Leopard compatibility, there's a very powerful new CGWindow API that will let you grab screen shots, window shots, or composites of any range of window layers.

如果您对 Leopard 的兼容性没有问题,那么有一个非常强大的新 CGWindow API,可以让您抓取屏幕截图、窗口截图或任何范围窗口层的合成。

http://developer.apple.com/samplecode/SonOfGrab/

http://developer.apple.com/samplecode/SonOfGrab/

回答by Andy Brice

Qt includes an example screenshot app in examples\desktop\screenshot. Qt works on a range of platforms, including MacOSX.

Qt 在 examples\desktop\screenshot 中包含一个示例屏幕截图应用程序。Qt 适用于多种平台,包括 MacOSX。

http://trolltech.com/products/qt/

http://trolltech.com/products/qt/

回答by pestophagous

The following might be helpful if you are attempting to accomplish this with C++ or python. Also, this would be even more helpful in the case that you want your programmatic method to be cross-platform portable. (Windows, Linux, Mac osx, and even beyond)

如果您尝试使用 C++ 或 python 完成此操作,以下内容可能会有所帮助。此外,如果您希望您的编程方法是跨平台可移植的,这将更有帮助。(Windows、Linux、Mac osx 甚至更高版本)

An earlier response mentions QT.

较早的回复提到了 QT。

In the same way that QT will allow you to capture and save a screenshot, so does another "competing" framework, namely wxWidgets. wxWidgets is a C++ framework, but it also provides python bindings via wxPython.

与 QT 允许您捕获和保存屏幕截图的方式相同,另一个“竞争”框架,即 wxWidgets 也是如此。wxWidgets 是一个 C++ 框架,但它也通过 wxPython 提供了 python 绑定。

To read more, use the following link, search the book for wxScreenDCand choose "Page 139" from the list of pages that match the search:

要阅读更多信息,请使用以下链接,在书中搜索wxScreenDC,然后从与搜索匹配的页面列表中选择“第 139 页”:

http://books.google.com/books?id=CyMsvtgnq0QC&vq="accessing+the+screen+with+wxScreendc"

http://books.google.com/books?id=CyMsvtgnq0QC&vq="accessing+the+screen+with+wxScreendc"

回答by stevechol

If you consider REALbasic, this is extremely easy to do with RB and the MBS Plugins. I've just written an application that does timed screenshots using RB and the MBS Plugins. You can read about it here: http://tektalkin.blogspot.com/2008/08/screenaudit-for-mac-osx.html

如果您考虑 REALbasic,则使用 RB 和 MBS 插件非常容易做到这一点。我刚刚编写了一个使用 RB 和 MBS 插件进行定时截图的应用程序。您可以在这里阅读:http: //tektalkin.blogspot.com/2008/08/screenaudit-for-mac-osx.html