Xamarin - 如何使用 xamarin mac 获取屏幕截图并将其保存在磁盘上?
Xamarin - How to get a screenshot and save it on disk using xamarin mac?
我在 Mac 上尝试使用 Xamarin 和 C# 抓取屏幕截图并将其保存在磁盘上。我写了下面的代码:
public static void TakeScreenshotAndSaveToDisk(string path)
{
var fullScreenBounds = NSScreen.MainScreen.Frame;
IntPtr ptr = CGWindowListCreateImage(fullScreenBounds, CGWindowListOption.OnScreenAboveWindow, 0, CGWindowImageOption.Default);
var cgImage = new CGImage(ptr);
var fileURL = new NSUrl(path, false);
var imageDestination = CGImageDestination.Create(new CGDataConsumer(fileURL), UTType.PNG, 1);
imageDestination.AddImage(cgImage);
imageDestination.Close();
imageDestination.Dispose();
fileURL.Dispose();
cgImage.Dispose();
}
方法执行并且文件出现在正确的位置。如果我尝试打开它,它将显示为空白。如果我在文件上单击 "Get Info",它将不会显示预览。
关闭我的应用程序后,图像可以打开并且 "Get Info" 显示预览。
我在这里做错了什么?在我看来,即使我在对象上调用 Dispose(),资源也没有释放。
谢谢。
CGImageDestination.Create
方法有 3 种不同的签名,如果您使用接受 NSUrl 而不是 CGDataConsumer
的签名,您应该不错。
var imageDestination = CGImageDestination.Create(fileURL, UTType.PNG, 1);
有了这个你不需要创建 CGDataConsumer
但如果你真的 want/need 到
var dataConsumer = new CGDataConsumer(fileURL);
var imageDestination = CGImageDestination.Create(dataConsumer, UTType.PNG, 1);
imageDestination.AddImage(cgImage);
imageDestination.Close();
dataConsumer.Dispose();
只需确保在保存文件后释放实例。
使用 using
方法:
using (var dataConsumer = new CGDataConsumer(fileURL))
{
var imageDestination = CGImageDestination.Create(dataConsumer, UTType.PNG, 1);
imageDestination.AddImage(cgImage);
imageDestination.Close();
}
注意:对于CGImageDestination
你不需要手动处理,Close
方法也会处理对象(基于文档).
public Boolean Close ()
Writes the images to the destination and disposes the object.
希望这对您有所帮助。-
我可能来晚了,但刚刚用 Xamarin.Mac 实现了 ,并且也可以在无头模式下使用:
https://gist.github.com/MarcosCobena/b4768bacc1a112a4f38a9d11a19f1251
它依赖于 "new" CoreGraphics 绑定(一些已经存在于 Xamarin.Mac,但内部或私有)来检测显示并枚举它们。最后,它为每个显示截取一个屏幕截图,将其保存为给定路径中的 PNG。
我在 Mac 上尝试使用 Xamarin 和 C# 抓取屏幕截图并将其保存在磁盘上。我写了下面的代码:
public static void TakeScreenshotAndSaveToDisk(string path)
{
var fullScreenBounds = NSScreen.MainScreen.Frame;
IntPtr ptr = CGWindowListCreateImage(fullScreenBounds, CGWindowListOption.OnScreenAboveWindow, 0, CGWindowImageOption.Default);
var cgImage = new CGImage(ptr);
var fileURL = new NSUrl(path, false);
var imageDestination = CGImageDestination.Create(new CGDataConsumer(fileURL), UTType.PNG, 1);
imageDestination.AddImage(cgImage);
imageDestination.Close();
imageDestination.Dispose();
fileURL.Dispose();
cgImage.Dispose();
}
方法执行并且文件出现在正确的位置。如果我尝试打开它,它将显示为空白。如果我在文件上单击 "Get Info",它将不会显示预览。 关闭我的应用程序后,图像可以打开并且 "Get Info" 显示预览。
我在这里做错了什么?在我看来,即使我在对象上调用 Dispose(),资源也没有释放。
谢谢。
CGImageDestination.Create
方法有 3 种不同的签名,如果您使用接受 NSUrl 而不是 CGDataConsumer
的签名,您应该不错。
var imageDestination = CGImageDestination.Create(fileURL, UTType.PNG, 1);
有了这个你不需要创建 CGDataConsumer
但如果你真的 want/need 到
var dataConsumer = new CGDataConsumer(fileURL);
var imageDestination = CGImageDestination.Create(dataConsumer, UTType.PNG, 1);
imageDestination.AddImage(cgImage);
imageDestination.Close();
dataConsumer.Dispose();
只需确保在保存文件后释放实例。
使用 using
方法:
using (var dataConsumer = new CGDataConsumer(fileURL))
{
var imageDestination = CGImageDestination.Create(dataConsumer, UTType.PNG, 1);
imageDestination.AddImage(cgImage);
imageDestination.Close();
}
注意:对于CGImageDestination
你不需要手动处理,Close
方法也会处理对象(基于文档).
public Boolean Close ()
Writes the images to the destination and disposes the object.
希望这对您有所帮助。-
我可能来晚了,但刚刚用 Xamarin.Mac 实现了
https://gist.github.com/MarcosCobena/b4768bacc1a112a4f38a9d11a19f1251
它依赖于 "new" CoreGraphics 绑定(一些已经存在于 Xamarin.Mac,但内部或私有)来检测显示并枚举它们。最后,它为每个显示截取一个屏幕截图,将其保存为给定路径中的 PNG。