Как сделать снимок экрана и сохранить его в файл в 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);   
        }

Не уверен, что он работает должным образом. Кто-нибудь может помочь?


person sann05    schedule 14.05.2016    source источник


Ответы (1)


Этот фрагмент будет захватывать текущий экран и сохранять результат в формате 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();
    }
}
person SushiHangover    schedule 14.05.2016