我正在尝试在 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();
}
Run Code Online (Sandbox Code Playgroud)
该方法执行并且文件出现在正确的位置。如果我尝试打开它,它将显示空白。如果我单击文件上的“获取信息”,它将不会显示预览。关闭应用程序后,可以打开图像并“获取信息”显示预览。
我在这里做错了什么?在我看来,即使我对对象调用 Dispose() ,资源也没有被释放。
谢谢。