[英]How to capture key down events at application level in 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.