public void TakeScreenshot(string screenshotPath)
 {
                 #if MAC
     DispatchService.GuiDispatch(delegate {
         try {
             IntPtr handle = CGDisplayCreateImage(MainDisplayID());
             CoreGraphics.CGImage screenshot = ObjCRuntime.Runtime.GetINativeObject <CoreGraphics.CGImage> (handle, true);
             AppKit.NSBitmapImageRep imgRep  = new AppKit.NSBitmapImageRep(screenshot);
             var imageData = imgRep.RepresentationUsingTypeProperties(AppKit.NSBitmapImageFileType.Png);
             imageData.Save(screenshotPath, true);
         } catch (Exception e) {
             Console.WriteLine(e);
             throw;
         }
     });
                 #endif
 }
Exemple #2
0
 public void TakeScreenshot(string screenshotPath)
 {
                 #if MAC
     Runtime.RunInMainThread(delegate {
         try {
             IntPtr handle = CGDisplayCreateImage(MainDisplayID());
             CoreGraphics.CGImage screenshot = ObjCRuntime.Runtime.GetINativeObject <CoreGraphics.CGImage> (handle, true);
             AppKit.NSBitmapImageRep imgRep  = new AppKit.NSBitmapImageRep(screenshot);
             var imageData = imgRep.RepresentationUsingTypeProperties(AppKit.NSBitmapImageFileType.Png);
             imageData.Save(screenshotPath, true);
         } catch (Exception e) {
             Console.WriteLine(e);
             throw;
         }
     });
                 #else
     Sync(delegate {
         try {
             using (var bmp = new System.Drawing.Bitmap(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width,
                                                        System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height)) {
                 using (var g = System.Drawing.Graphics.FromImage(bmp))
                 {
                     g.CopyFromScreen(System.Windows.Forms.Screen.PrimaryScreen.Bounds.X,
                                      System.Windows.Forms.Screen.PrimaryScreen.Bounds.Y,
                                      0, 0,
                                      bmp.Size,
                                      System.Drawing.CopyPixelOperation.SourceCopy);
                 }
                 bmp.Save(screenshotPath);
             }
             return(null);
         } catch (Exception e) {
             Console.WriteLine(e);
             throw;
         }
     });
                 #endif
 }
        public static void TestCapture()
        {
            Foundation.NSNumber mainScreen = (Foundation.NSNumber)AppKit.NSScreen.MainScreen.DeviceDescription["NSScreenNumber"];

            using (CoreGraphics.CGImage cgImage = CreateImage(mainScreen.UInt32Value))
            {
                // https://stackoverflow.com/questions/17334786/get-pixel-from-the-screen-screenshot-in-max-osx/17343305#17343305

                // Get byte-array from CGImage
                // https://gist.github.com/zhangao0086/5fafb1e1c0b5d629eb76

                AppKit.NSBitmapImageRep bitmapRep = new AppKit.NSBitmapImageRep(cgImage);

                // var imageData = bitmapRep.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: [:])
                Foundation.NSData imageData = bitmapRep.RepresentationUsingTypeProperties(AppKit.NSBitmapImageFileType.Png);

                long   len   = imageData.Length;
                byte[] bytes = new byte[len];
                System.Runtime.InteropServices.GCHandle pinnedArray = System.Runtime.InteropServices.GCHandle.Alloc(bytes, System.Runtime.InteropServices.GCHandleType.Pinned);
                System.IntPtr pointer = pinnedArray.AddrOfPinnedObject();
                // Do your stuff...
                imageData.GetBytes(pointer, new System.IntPtr(len));
                pinnedArray.Free();

                using (AppKit.NSImage nsImage = new AppKit.NSImage(cgImage, new System.Drawing.SizeF(cgImage.Width, cgImage.Height)))
                {
                    // ImageView.Image = nsImage;
                    // And now ? How to get the image bytes ?

                    // https://theconfuzedsourcecode.wordpress.com/2016/02/24/convert-android-bitmap-image-and-ios-uiimage-to-byte-array-in-xamarin/
                    // https://stackoverflow.com/questions/5645157/nsimage-from-byte-array
                    // https://stackoverflow.com/questions/53060723/nsimage-source-from-byte-array-cocoa-app-xamarin-c-sharp
                    // https://gist.github.com/zhangao0086/5fafb1e1c0b5d629eb76
                    // https://www.quora.com/What-is-a-way-to-convert-UIImage-to-a-byte-array-in-Swift?share=1
                    // https://stackoverflow.com/questions/17112314/converting-uiimage-to-byte-array
                } // End Using nsImage
            }     // End Using cgImage
        }         // End Sub TestCapture
		public void TakeScreenshot (string screenshotPath)
		{
			#if MAC
			DispatchService.GuiDispatch (delegate {
				try {
					IntPtr handle = CGDisplayCreateImage (MainDisplayID ());
					CoreGraphics.CGImage screenshot = ObjCRuntime.Runtime.GetINativeObject <CoreGraphics.CGImage> (handle, true);
					AppKit.NSBitmapImageRep imgRep =  new AppKit.NSBitmapImageRep (screenshot);
					var imageData = imgRep.RepresentationUsingTypeProperties (AppKit.NSBitmapImageFileType.Png);
					imageData.Save (screenshotPath, true);
				} catch (Exception e) {
					Console.WriteLine (e);
					throw;
				}
			});
			#endif
		}
		public void TakeScreenshot (string screenshotPath)
		{
			#if MAC
			Runtime.RunInMainThread (delegate {
				try {
					IntPtr handle = CGDisplayCreateImage (MainDisplayID ());
					CoreGraphics.CGImage screenshot = ObjCRuntime.Runtime.GetINativeObject <CoreGraphics.CGImage> (handle, true);
					AppKit.NSBitmapImageRep imgRep =  new AppKit.NSBitmapImageRep (screenshot);
					var imageData = imgRep.RepresentationUsingTypeProperties (AppKit.NSBitmapImageFileType.Png);
					imageData.Save (screenshotPath, true);
				} catch (Exception e) {
					Console.WriteLine (e);
					throw;
				}
			});
			#else
			Sync (delegate {
				try {
					using (var bmp = new System.Drawing.Bitmap (System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width,
						System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height)) {
						using (var g = System.Drawing.Graphics.FromImage(bmp))
						{
							g.CopyFromScreen(System.Windows.Forms.Screen.PrimaryScreen.Bounds.X,
								System.Windows.Forms.Screen.PrimaryScreen.Bounds.Y,
								0, 0,
								bmp.Size,
								System.Drawing.CopyPixelOperation.SourceCopy);
						}
						bmp.Save(screenshotPath);
					}
					return null;
				} catch (Exception e) {
					Console.WriteLine (e);
					throw;
				}
			});
			#endif
		}