Exemplo n.º 1
0
        public IBitmapFrame Capture()
        {
            var bmp = _imagePool.Get();

            try
            {
                using (var editor = bmp.GetEditor())
                {
                    OnCapture(editor.Graphics);

                    if (_includeCursor)
                    {
                        MouseCursor.Draw(editor.Graphics, _transform);
                    }
                }

                return(bmp);
            }
            catch
            {
                bmp.Dispose();

                return(RepeatFrame.Instance);
            }
        }
Exemplo n.º 2
0
        public IBitmapFrame Capture()
        {
            var bmp = _imagePool.Get();

            try
            {
                using (var editor = bmp.GetEditor())
                {
                    if (_outsideBounds)
                    {
                        editor.Graphics.Clear(Color.Transparent);
                    }

                    editor.Graphics.CopyFromScreen(_region.Location,
                                                   Point.Empty,
                                                   _region.Size,
                                                   CopyPixelOperation.SourceCopy);

                    if (_includeCursor)
                    {
                        MouseCursor.Draw(editor.Graphics, _transform);
                    }
                }

                return(bmp);
            }
            catch
            {
                bmp.Dispose();

                return(RepeatFrame.Instance);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Captures an Image.
        /// </summary>
        public Bitmap Capture()
        {
            var bmp = new Bitmap(Width, Height);

            try
            {
                using (var g = Graphics.FromImage(bmp))
                {
                    OnCapture(g);

                    if (_includeCursor)
                    {
                        MouseCursor.Draw(g, _transform);
                    }
                }

                lastImage = bmp;

                return(bmp);
            }
            catch
            {
                if (lastImage != null)
                {
                    bmp.Dispose();

                    return(lastImage);
                }

                return(bmp);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Capture transparent Screenshot of a Window.
        /// </summary>
        /// <param name="Window">The <see cref="Window"/> to Capture.</param>
        /// <param name="IncludeCursor">Whether to include Mouse Cursor.</param>
        public static Bitmap CaptureTransparent(Window Window, bool IncludeCursor = false)
        {
            var backdrop = new Form
            {
                AllowTransparency = true,
                BackColor         = Color.White,
                FormBorderStyle   = FormBorderStyle.None,
                ShowInTaskbar     = false,
                Opacity           = 0
            };

            var r = new RECT();

            const int extendedFrameBounds = 0;

            if (DwmGetWindowAttribute(Window.Handle, extendedFrameBounds, ref r, Marshal.SizeOf <RECT>()) != 0)
            {
                // DwmGetWindowAttribute() failed, usually means Aero is disabled so we fall back to GetWindowRect()
                User32.GetWindowRect(Window.Handle, out r);
            }

            var R = r.ToRectangle();

            // Add a 100px margin for window shadows. Excess transparency is trimmed out later
            R.Inflate(100, 100);

            // This check handles if the window is outside of the visible screen
            R.Intersect(WindowProvider.DesktopRectangle);

            ShowWindow(backdrop.Handle, 4);
            SetWindowPos(backdrop.Handle, Window.Handle,
                         R.Left, R.Top,
                         R.Width, R.Height,
                         SetWindowPositionFlags.NoActivate);
            backdrop.Opacity = 1;
            Application.DoEvents();

            // Capture screenshot with white background
            using (var whiteShot = Capture(R))
            {
                backdrop.BackColor = Color.Black;
                Application.DoEvents();

                // Capture screenshot with black background
                using (var blackShot = Capture(R))
                {
                    backdrop.Dispose();

                    var transparentImage = Extensions.DifferentiateAlpha(whiteShot, blackShot);

                    if (IncludeCursor)
                    {
                        using (var g = Graphics.FromImage(transparentImage))
                            MouseCursor.Draw(g, P => new Point(P.X - R.X, P.Y - R.Y));
                    }

                    return(transparentImage.CropEmptyEdges());
                }
            }
        }
Exemplo n.º 5
0
        void OnCapture()
        {
            if (!_window.IsAlive)
            {
                throw new WindowClosedException();
            }

            var rect  = _window.Rectangle.Even();
            var ratio = Math.Min((float)Width / rect.Width, (float)Height / rect.Height);

            var resizeWidth  = (int)(rect.Width * ratio);
            var resizeHeight = (int)(rect.Height * ratio);

            var hdcDest = _dcTarget.GetDC();

            void ClearRect(RECT Rect)
            {
                User32.FillRect(hdcDest, ref Rect, IntPtr.Zero);
            }

            if (Width != resizeWidth)
            {
                ClearRect(new RECT
                {
                    Left   = resizeWidth,
                    Right  = Width,
                    Bottom = Height
                });
            }
            else if (Height != resizeHeight)
            {
                ClearRect(new RECT
                {
                    Top    = resizeHeight,
                    Right  = Width,
                    Bottom = Height
                });
            }

            Gdi32.StretchBlt(hdcDest, 0, 0, resizeWidth, resizeHeight,
                             _hdcSrc, rect.X, rect.Y, rect.Width, rect.Height,
                             (int)CopyPixelOperation.SourceCopy);

            if (_includeCursor)
            {
                MouseCursor.Draw(hdcDest, PointTransform);
            }
        }
Exemplo n.º 6
0
        public IBitmapFrame Capture()
        {
            Gdi32.BitBlt(_hdcDest, 0, 0, _region.Width, _region.Height,
                         _hdcSrc, _region.X, _region.Y,
                         (int)CopyPixelOperation.SourceCopy);

            var img = new OneTimeFrame(Image.FromHbitmap(_hBitmap));

            if (_includeCursor)
            {
                using (var editor = img.GetEditor())
                    MouseCursor.Draw(editor.Graphics, _transform);
            }

            return(img);
        }
Exemplo n.º 7
0
        static Bitmap CaptureInternal(Rectangle Region, bool IncludeCursor = false)
        {
            var bmp = new Bitmap(Region.Width, Region.Height);

            using (var g = Graphics.FromImage(bmp))
            {
                g.CopyFromScreen(Region.Location, Point.Empty, Region.Size, CopyPixelOperation.SourceCopy);

                if (IncludeCursor)
                {
                    MouseCursor.Draw(g, P => new Point(P.X - Region.X, P.Y - Region.Y));
                }

                g.Flush();
            }

            return(bmp);
        }
Exemplo n.º 8
0
        public IEditableFrame Capture()
        {
            // Update Location
            _region.Location = _locationFunc();

            Gdi32.BitBlt(_hdcDest, 0, 0, _region.Width, _region.Height,
                         _hdcSrc, _region.X, _region.Y,
                         (int)CopyPixelOperation.SourceCopy);

            var img = new GraphicsEditor(Image.FromHbitmap(_hBitmap));

            if (_includeCursor)
            {
                MouseCursor.Draw(img, _transform);
            }

            return(img);
        }
Exemplo n.º 9
0
        private static Bitmap CaptureInternal(Rectangle region, bool includeCursor = false)
        {
            var bitmap = new Bitmap(region.Width, region.Height);

            using (var graphics = Graphics.FromImage(bitmap))
            {
                graphics.CopyFromScreen(region.Location, Point.Empty, region.Size, CopyPixelOperation.SourceCopy);

                if (includeCursor)
                {
                    MouseCursor.Draw(graphics, point => new Point(point.X - region.X, point.Y - region.Y));
                }

                graphics.Flush();
            }

            return(bitmap);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Capture transparent Screenshot of a Window.
        /// </summary>
        /// <param name="Window">The <see cref="IWindow"/> to Capture.</param>
        /// <param name="IncludeCursor">Whether to include Mouse Cursor.</param>
        public static IBitmapImage CaptureTransparent(IWindow Window, bool IncludeCursor = false)
        {
            if (Window == null)
            {
                throw new ArgumentNullException(nameof(Window));
            }

            var backdrop = new WindowScreenShotBackdrop(Window);

            backdrop.ShowWhite();

            var r = backdrop.Rectangle;

            // Capture screenshot with white background
            using (var whiteShot = CaptureInternal(r))
            {
                backdrop.ShowBlack();

                // Capture screenshot with black background
                using (var blackShot = CaptureInternal(r))
                {
                    backdrop.Dispose();

                    var transparentImage = Extensions.DifferentiateAlpha(whiteShot, blackShot);

                    if (transparentImage == null)
                    {
                        return(null);
                    }

                    var platformServices = ServiceProvider.Get <IPlatformServices>();

                    // Include Cursor only if within window
                    if (IncludeCursor && r.Contains(platformServices.CursorPosition))
                    {
                        using (var g = Graphics.FromImage(transparentImage))
                            MouseCursor.Draw(g, P => new Point(P.X - r.X, P.Y - r.Y));
                    }

                    return(new DrawingImage(transparentImage.CropEmptyEdges()));
                }
            }
        }
Exemplo n.º 11
0
        public IEditableFrame Capture()
        {
            // Update Location
            _region.Location = _locationFunc();

            var hdcDest = _dcTarget.GetDC();

            Gdi32.BitBlt(hdcDest, 0, 0, _region.Width, _region.Height,
                         _hdcSrc, _region.X, _region.Y,
                         (int)CopyPixelOperation.SourceCopy);

            if (_includeCursor)
            {
                MouseCursor.Draw(hdcDest, PointTransform);
            }

            var img = _dcTarget.GetEditableFrame();

            return(img);
        }
Exemplo n.º 12
0
        public IEditableFrame Capture()
        {
            try
            {
                OnCapture();

                var img = new GraphicsEditor(Image.FromHbitmap(_hBitmap));

                if (_includeCursor)
                {
                    MouseCursor.Draw(img, _transform);
                }

                return(img);
            }
            catch (Exception e) when(!(e is WindowClosedException))
            {
                return(RepeatFrame.Instance);
            }
        }
Exemplo n.º 13
0
        public IBitmapFrame Capture()
        {
            try
            {
                OnCapture();

                var img = new OneTimeFrame(Image.FromHbitmap(_hBitmap));

                if (_includeCursor)
                {
                    using (var editor = img.GetEditor())
                        MouseCursor.Draw(editor.Graphics, _transform);
                }

                return(img);
            }
            catch (Exception e) when(!(e is WindowClosedException))
            {
                return(RepeatFrame.Instance);
            }
        }