Beispiel #1
0
        internal static NSImage TakeSnapshot(NSView view, object data)
        {
            NSImage snapshot = null;

            if (data is IDataObject idata)
            {
                if (idata.GetData("DragAndDropImage") is Drawing.Image image)
                {
                    snapshot = image.ToNSImage();
                }
            }

            if (snapshot == null && view is MonoView)
            {
                snapshot = CreateSwatch(new CGSize(32, 22));
            }

            if (snapshot == null)
            {
                var b = view.BitmapImageRepForCachingDisplayInRect(view.Bounds);
                view.CacheDisplay(view.Bounds, b);

                snapshot = new NSImage(view.Bounds.Size);
                snapshot.AddRepresentation(b);
            }
            return(snapshot);
        }
        public static XIR.Image RemoteRepresentation(this NSView nsview)
        {
            var bitmap = nsview.BitmapImageRepForCachingDisplayInRect(nsview.Bounds);

            if (bitmap == null)
            {
                return(null);
            }
            nsview.CacheDisplay(nsview.Bounds, bitmap);
            return(bitmap.RemoteRepresentation());
        }
Beispiel #3
0
        protected override void UpdateCapturedImage()
        {
            if (view != null && layer == null)
            {
                var bitmap = view.BitmapImageRepForCachingDisplayInRect(view.Bounds);
                if (bitmap == null)
                {
                    return;
                }

                view.CacheDisplay(view.Bounds, bitmap);
                var data = bitmap.RepresentationUsingTypeProperties(NSBitmapImageFileType.Png);
                CapturedImage = data.ToArray();
            }
            else if (layer != null)
            {
                var  scale       = layer.ContentsScale;
                nint h           = (nint)(layer.Bounds.Height * scale);
                nint w           = (nint)(layer.Bounds.Width * scale);
                nint bytesPerRow = w * 4;

                if (h <= 0 || w <= 0)
                {
                    return;
                }

                using (var colorSpace = CGColorSpace.CreateGenericRgb())
                    using (var context = new CGBitmapContext(IntPtr.Zero, w, h, 8, bytesPerRow, colorSpace, CGImageAlphaInfo.PremultipliedLast)) {
                        // Apply a flipping transform because layers are apparently weird.
                        var transform = new CGAffineTransform(scale, 0, 0, -scale, 0, h);
                        context.ConcatCTM(transform);

                        layer.RenderInContext(context);

                        using (var image = context.ToImage())
                            using (var bitmap = new NSBitmapImageRep(image)) {
                                var data = bitmap.RepresentationUsingTypeProperties(NSBitmapImageFileType.Png);
                                CapturedImage = data.ToArray();
                            }
                    }
            }
        }