Example #1
0
        /// <summary>
        ///     This method takes the actual capture of the document (frame)
        /// </summary>
        /// <param name="documentContainer">DocumentContainer</param>
        /// <param name="contentWindowDetails">Needed for referencing the location of the frame</param>
        /// <param name="graphicsTarget">Graphics</param>
        /// <returns>Bitmap with the capture</returns>
        private static void DrawDocument(DocumentContainer documentContainer, IInteropWindow contentWindowDetails, Graphics graphicsTarget)
        {
            documentContainer.SetAttribute("scroll", 1);

            //Get Browser Window Width & Height
            var pageWidth  = documentContainer.ScrollWidth;
            var pageHeight = documentContainer.ScrollHeight;

            if (pageWidth * pageHeight == 0)
            {
                Log.Warn().WriteLine("Empty page for DocumentContainer {0}: {1}", documentContainer.Name, documentContainer.Url);
                return;
            }

            //Get Screen Width & Height (this is better as the WindowDetails.ClientRectangle as the real visible parts are there!
            var viewportWidth  = documentContainer.ClientWidth;
            var viewportHeight = documentContainer.ClientHeight;

            if (viewportWidth * viewportHeight == 0)
            {
                Log.Warn().WriteLine("Empty viewport for DocumentContainer {0}: {1}", documentContainer.Name, documentContainer.Url);
                return;
            }

            // Store the current location so we can set the browser back and use it for the mouse cursor
            var startLeft = documentContainer.ScrollLeft;
            var startTop  = documentContainer.ScrollTop;

            Log.Debug().WriteLine("Capturing {4} with total size {0},{1} displayed with size {2},{3}", pageWidth, pageHeight, viewportWidth, viewportHeight, documentContainer.Name);

            // Variable used for looping horizontally
            var horizontalPage = 0;

            // The location of the browser, used as the destination into the bitmap target
            var targetOffset = new NativePoint();

            // Loop of the pages and make a copy of the visible viewport
            while (horizontalPage * viewportWidth < pageWidth)
            {
                // Scroll to location
                documentContainer.ScrollLeft = viewportWidth * horizontalPage;
                targetOffset = targetOffset.ChangeX(documentContainer.ScrollLeft);

                // Variable used for looping vertically
                var verticalPage = 0;
                while (verticalPage * viewportHeight < pageHeight)
                {
                    // Scroll to location
                    documentContainer.ScrollTop = viewportHeight * verticalPage;
                    //Shoot visible window
                    targetOffset = targetOffset.ChangeY(documentContainer.ScrollTop);

                    // Draw the captured fragment to the target, but "crop" the scrollbars etc while capturing
                    var viewPortSize    = new Size(viewportWidth, viewportHeight);
                    var clientRectangle = new NativeRect(documentContainer.SourceLocation, viewPortSize);
                    var fragment        = contentWindowDetails.PrintWindow();
                    if (fragment != null)
                    {
                        Log.Debug().WriteLine("Captured fragment size: {0}x{1}", fragment.Width, fragment.Height);
                        try
                        {
                            // cut all junk, due to IE "border" we need to remove some parts
                            var viewportRect = documentContainer.ViewportRectangle;
                            if (!viewportRect.IsEmpty)
                            {
                                Log.Debug().WriteLine("Cropping to viewport: {0}", viewportRect);
                                BitmapHelper.Crop(ref fragment, ref viewportRect);
                            }
                            Log.Debug().WriteLine("Cropping to clientRectangle: {0}", clientRectangle);
                            // Crop to clientRectangle
                            if (BitmapHelper.Crop(ref fragment, ref clientRectangle))
                            {
                                var targetLocation = new NativePoint(documentContainer.DestinationLocation.X, documentContainer.DestinationLocation.Y);
                                Log.Debug().WriteLine("Fragment targetLocation is {0}", targetLocation);
                                targetLocation = targetLocation.Offset(targetOffset);
                                Log.Debug().WriteLine("After offsetting the fragment targetLocation is {0}", targetLocation);
                                Log.Debug().WriteLine("Drawing fragment of size {0} to {1}", fragment.Size, targetLocation);
                                graphicsTarget.DrawImage(fragment, targetLocation);
                                graphicsTarget.Flush();
                            }
                            else
                            {
                                // somehow we are capturing nothing!?
                                Log.Warn().WriteLine("Crop of {0} failed?", documentContainer.Name);
                                break;
                            }
                        }
                        finally
                        {
                            fragment.Dispose();
                        }
                    }
                    else
                    {
                        Log.Warn().WriteLine("Capture of {0} failed!", documentContainer.Name);
                    }
                    verticalPage++;
                }
                horizontalPage++;
            }
            // Return to where we were
            documentContainer.ScrollLeft = startLeft;
            documentContainer.ScrollTop  = startTop;
        }