Example #1
0
        public async void GetFullScreenshotAsync()
        {
            GetDocHeight();
            await PutTaskDelay();

            name = myBrowser.Address.GetHostname().Replace("www.", null).Replace(".", "_");
            int  scrollHeight = DocHeight;
            Size size         = myBrowser.ClientRectangle.Size;
            int  height       = size.Height;
            int  width        = size.Width;
            int  scrollLeft   = scrollHeight;
            int  num          = 0;
            bool atBottom     = false;

            try
            {
                if (scrollHeight != 0)
                {
                    myBrowser.Xecute("(function() { document.documentElement.style.overflow = 'hidden'; })();");
                    while (!atBottom)
                    {
                        if (scrollLeft > height)
                        {
                            num++;
                            tempname = string.Concat(name, "_", num, ".png");
                            if (num != 1)
                            {
                                myBrowser.Xecute(string.Concat("(function() { window.scroll(0,", num * height - 30, "); })();"));
                            }
                            using (Bitmap Vs = GetCurrentViewScreenshot())
                            {
                                if (num == 1)
                                {
                                    SaveScreenshot(Vs, tempname);
                                }
                                else
                                {
                                    Rectangle rectangle = new Rectangle(new Point(0, 30), new Size(width, height));
                                    using (Bitmap bitmap = new Bitmap(rectangle.Width, rectangle.Height))
                                    {
                                        using (Graphics graphic = Graphics.FromImage(bitmap))
                                        {
                                            graphic.DrawImage(Vs, new Rectangle(0, 0, bitmap.Width, bitmap.Height), rectangle, GraphicsUnit.Pixel);
                                            SaveScreenshot(bitmap, tempname);
                                        }
                                    }
                                }
                            }
                            if (myBrowser.Address.StartsWith("http://www.google") || myBrowser.Address.StartsWith("https://www.google") || myBrowser.Address.StartsWith("http://google"))
                            {
                                myBrowser.Xecute("(function() { var elements = document.querySelectorAll('*'); for (var i = 0; i < elements.length; i++) { var position = window.getComputedStyle(elements[i]).position; if (position === 'fixed') { elements[i].style.visibility = 'hidden'; } } })(); ");
                            }
                        }
                        else
                        {
                            myBrowser.Xecute(string.Concat("(function() { window.scrollBy(0,", height, "); })();"));
                            atBottom = true;
                            num++;
                            tempname = string.Concat(name, "_", num, ".png");
                            await PutTaskDelay();

                            Rectangle rectangle = new Rectangle(new Point(0, height - scrollLeft - 30), new Size(width, scrollLeft + 30));
                            using (Bitmap currentViewScreenshot = GetCurrentViewScreenshot())
                            {
                                using (Bitmap bitmap = new Bitmap(rectangle.Width, rectangle.Height))
                                {
                                    using (Graphics graphic = Graphics.FromImage(bitmap))
                                    {
                                        graphic.DrawImage(currentViewScreenshot, new Rectangle(0, 0, bitmap.Width, bitmap.Height), rectangle, GraphicsUnit.Pixel);
                                        SaveScreenshot(bitmap, tempname);
                                    }
                                }
                            }
                        }
                        scrollLeft = scrollLeft - height;
                    }
                    myBrowser.Xecute("(function() { document.documentElement.style.overflow = 'auto'; })();");
                    myBrowser.Xecute("javascript:var s = function() { document.body.scrollTop = document.documentElement.scrollTop = 0;}; s();");
                    myBrowser.Xecute("(function() { var elements = document.querySelectorAll('*'); for (var i = 0; i < elements.length; i++) { var position = window.getComputedStyle(elements[i]).position; if (position === 'fixed') { elements[i].style.visibility = 'visible'; } } })(); ");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Cannot take screenshot", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
Example #2
0
 public void alert(string text)
 {
     MyBrowser.Xecute("alert('" + text + "')");
 }