2016-05-14 2 views
0

Я не опытный разработчик, и у меня проблема.Как захватить экран и сохранить его в файл в 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); 
     } 

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

ответ

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(); 
    } 
} 
Смежные вопросы