繁体   English   中英

如何捕获屏幕并将其保存到Xamarin.Mac中的文件

[英]How to capture the screen and save it to the file in Xamarin.Mac

我不是经验丰富的开发人员,所以有问题。

我不明白如何制作屏幕截图并将其保存到文件中。

[DllImport("/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/CoreGraphics.framework/CoreGraphics")]
    private static extern IntPtr CGWindowListCreateImage(RectangleF screenBounds, CGWindowListOption windowOption, uint windowID, GWindowImageOption imageOption);
partial void ButtonClicked (Foundation.NSObject sender) {

            IntPtr screenShot = CGWindowListCreateImage ((RectangleF)NSScreen.MainScreen.Frame, CGWindowListOption.IncludingWindow,
                0, CGWindowImageOption.Default);

            CGImage img = new CGImage(screenShot);
            NSBitmapImageRep imgRep = new NSBitmapImageRep(img);

            NSImage imgf = new NSImage(img, NSScreen.MainScreen.Frame.Size);   
        }

不确定它是否正常运行。 有人可以帮忙吗?

该代码段将捕获当前屏幕,并将结果作为PNG保存到桌面:


[DllImport("/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/CoreGraphics.framework/CoreGraphics")]
static extern IntPtr CGWindowListCreateImage(CGRect screenBounds, CGWindowListOption windowOption, uint windowID, CGWindowImageOption imageOption);

public override void ViewDidLoad()
{
    base.ViewDidLoad();

    using (var pool = new NSAutoreleasePool())
    {
        CGRect fullScreenBounds = NSScreen.MainScreen.Frame;
        IntPtr imageRef = CGWindowListCreateImage(fullScreenBounds, CGWindowListOption.All, 0, CGWindowImageOption.Default);
        var cgImage = new CGImage(imageRef);
        var filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "StackOverflow.png");
        var fileURL = new NSUrl(filePath, false);
        var imageDestination = CGImageDestination.Create(fileURL, UTType.PNG, 1);
        imageDestination.AddImage(cgImage);
        imageDestination.Close();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM