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

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

            if (pageWidth * pageHeight == 0)
            {
                LOG.WarnFormat("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!
            int viewportWidth  = documentContainer.ClientWidth;
            int viewportHeight = documentContainer.ClientHeight;

            if (viewportWidth * viewportHeight == 0)
            {
                LOG.WarnFormat("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
            int startLeft = documentContainer.ScrollLeft;
            int startTop  = documentContainer.ScrollTop;

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

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

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

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

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

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