Ejemplo n.º 1
0
        public void BitmapStitcher_Default()
        {
            using (var bitmapStitcher = new BitmapStitcher())
            {
                bitmapStitcher
                .AddBitmap(BitmapHelper.LoadBitmap(@"TestFiles\scroll0.png"))
                .AddBitmap(BitmapHelper.LoadBitmap(@"TestFiles\scroll35.png"))
                .AddBitmap(BitmapHelper.LoadBitmap(@"TestFiles\scroll70.png"))
                .AddBitmap(BitmapHelper.LoadBitmap(@"TestFiles\scroll105.png"))
                .AddBitmap(BitmapHelper.LoadBitmap(@"TestFiles\scroll124.png"));

                using (var completedBitmap = bitmapStitcher.Result())
                {
                    completedBitmap.Save("scroll.png", ImageFormat.Png);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Start the capture
        /// </summary>
        /// <returns>Bitmap</returns>
        public Bitmap Capture()
        {
            if (_windowScroller.NeedsFocus())
            {
                User32Api.SetForegroundWindow(_windowScroller.ScrollBarWindow.Handle);
                Application.DoEvents();
                Thread.Sleep(Delay);
                Application.DoEvents();
            }

            // Find the area which is scrolling

            // 1. Take the client bounds
            var clientBounds = _windowScroller.ScrollBarWindow.GetInfo().ClientBounds;

            // Use a region for steps 2 and 3
            using (var region = new Region(clientBounds))
            {
                // 2. exclude the children, if any
                foreach (var interopWindow in _windowScroller.ScrollBarWindow.GetChildren())
                {
                    region.Exclude(interopWindow.GetInfo().Bounds);
                }
                // 3. exclude the scrollbar, if it can be found
                if (_windowScroller.ScrollBar.HasValue)
                {
                    region.Exclude(_windowScroller.ScrollBar.Value.Bounds);
                }
                // Get the bounds of the region
                using (var screenGraphics = Graphics.FromHwnd(User32Api.GetDesktopWindow()))
                {
                    var rectangleF = region.GetBounds(screenGraphics);
                    clientBounds = new NativeRect((int)rectangleF.X, (int)rectangleF.Y, (int)rectangleF.Width, (int)rectangleF.Height);
                }
            }

            if (clientBounds.Width * clientBounds.Height <= 0)
            {
                return(null);
            }
            // Move the window to the start
            _windowScroller.Start();

            // Register a keyboard hook to make it possible to ESC the capturing
            var breakScroll  = false;
            var keyboardHook = KeyboardHook.KeyboardEvents
                               .Where(args => args.Key == VirtualKeyCodes.ESCAPE)
                               .Subscribe(args =>
            {
                args.Handled = true;
                breakScroll  = true;
            });
            Bitmap resultImage = null;

            try
            {
                // A delay to make the window move
                Application.DoEvents();
                Thread.Sleep(Delay);
                Application.DoEvents();

                if (_windowScroller.IsAtStart)
                {
                    using (var bitmapStitcher = new BitmapStitcher())
                    {
                        bitmapStitcher.AddBitmap(WindowCapture.CaptureRectangle(clientBounds));

                        // Loop as long as we are not at the end yet
                        while (!_windowScroller.IsAtEnd && !breakScroll)
                        {
                            // Next "page"
                            _windowScroller.Next();
                            // Wait a bit, so the window can update
                            Application.DoEvents();
                            Thread.Sleep(Delay);
                            Application.DoEvents();
                            // Capture inside loop
                            bitmapStitcher.AddBitmap(WindowCapture.CaptureRectangle(clientBounds));
                        }
                        resultImage = bitmapStitcher.Result();
                    }
                }
                else
                {
                    resultImage = WindowCapture.CaptureRectangle(clientBounds);
                }
            }
            catch (Exception ex)
            {
                Log.Error().WriteLine(ex);
            }
            finally
            {
                // Remove hook for escape
                keyboardHook.Dispose();
                // Try to reset location
                _windowScroller.Reset();
            }

            return(resultImage);
        }