Esempio n. 1
0
        public WindowMain()
        {
            this.Loaded += delegate
            {
                Width  = 400;
                Height = 300;

                browserView = new WPFBrowserView(BrowserFactory.Create(BrowserType.LIGHTWEIGHT));
                Browser browser = browserView.Browser;


                // #1 Set browser initial size
                browserView.Browser.SetSize(1280, 1024);

                // #2 Load web page and wait until web page is loaded completely.
                ManualResetEvent          resetEvent = new ManualResetEvent(false);
                FinishLoadingFrameHandler listener   = new FinishLoadingFrameHandler((object sender, FinishLoadingEventArgs e) =>
                {
                    if (e.IsMainFrame)
                    {
                        resetEvent.Set();
                    }
                });
                browser.FinishLoadingFrameEvent += listener;
                try
                {
                    browser.LoadURL("teamdev.com/dotnetbrowser");
                    resetEvent.WaitOne(new TimeSpan(0, 0, 45));
                }
                finally
                {
                    browser.FinishLoadingFrameEvent -= listener;
                }

                // #3 Set the required document size.
                JSValue documentHeight = browserView.Browser.ExecuteJavaScriptAndReturnValue(
                    "Math.max(document.body.scrollHeight, " +
                    "document.documentElement.scrollHeight, document.body.offsetHeight, " +
                    "document.documentElement.offsetHeight, document.body.clientHeight, " +
                    "document.documentElement.clientHeight);");
                JSValue documentWidth = browserView.Browser.ExecuteJavaScriptAndReturnValue(
                    "Math.max(document.body.scrollWidth, " +
                    "document.documentElement.scrollWidth, document.body.offsetWidth, " +
                    "document.documentElement.offsetWidth, document.body.clientWidth, " +
                    "document.documentElement.clientWidth);");

                int scrollBarSize = 25;

                int viewWidth  = (int)documentWidth.GetNumber() + scrollBarSize;
                int viewHeight = (int)documentHeight.GetNumber() + scrollBarSize;

                // #4 Register OnRepaint to get notifications
                // about paint events. We expect that web page will be completely rendered twice:
                // 1. When its size is updated.
                // 2. When HTML content is loaded and displayed.
                ManualResetEvent waitEvent   = new ManualResetEvent(false);
                DrawingView      drawingView = (DrawingView)browserView.GetInnerView();
                drawingView.OnRepaint += delegate(object sender, OnRepaintEventArgs e)
                {
                    // Make sure that all view content has been repainted.
                    if (e.UpdatedRect.Size.Equals(e.ClientSize))
                    {
                        waitEvent.Set();
                    }
                };
                browserView.Browser.SetSize(viewWidth, viewHeight);
                // #5 Wait until Chromium renders web page content.
                waitEvent.WaitOne();
                // #6 Save Image of the loaded web page into a PNG file.
                Dispatcher.BeginInvoke((Action)(() =>
                {
                    //Create save file dialog
                    SaveFileDialog saveFileDialog = new SaveFileDialog();
                    saveFileDialog.Filter = "PNG files (*.PNG)|*.PNG|All files (*.*)|*.*";
                    saveFileDialog.FilterIndex = 1;
                    saveFileDialog.RestoreDirectory = true;

                    //Make sure that file should be save
                    if (saveFileDialog.ShowDialog() == true)
                    {
                        //Format name of file
                        string imageFileName = saveFileDialog.FileName.ToString().Replace("%.png%", "");
                        browserView.GetImage().Save(imageFileName, ImageFormat.Png);
                    }
                }));
            };
        }
Esempio n. 2
0
        public WindowMain()
        {
            this.Loaded += delegate
            {
                int viewHeight = 20000;
                // Disables GPU process and changes maximum texture size
                // value from default 16384 to viewHeight. The maximum texture size value
                // indicates the maximum height of the canvas where Chromium
                // renders web page's content. If the web page's height
                // exceeds the maximum texture size, the part of outsize the
                // texture size will not be drawn and will be filled with
                // black color.
                String[] switches =
                {
                    "--disable-gpu",
                    "--disable-gpu-compositing",
                    "--max-texture-size=" + viewHeight
                };
                BrowserPreferences.SetChromiumSwitches(switches);

                SizeToContent = System.Windows.SizeToContent.WidthAndHeight;

                browserView = new WPFBrowserView(BrowserFactory.Create(BrowserType.LIGHTWEIGHT));
                //      Content = browserView;

                Browser browser = browserView.Browser;


                // #1 Set browser initial size
                browserView.Browser.SetSize(1280, 1024);

                // #2 Load web page and wait until web page is loaded completely.
                ManualResetEvent          resetEvent = new ManualResetEvent(false);
                FinishLoadingFrameHandler listener   = new FinishLoadingFrameHandler((object sender, FinishLoadingEventArgs e) =>
                {
                    if (e.IsMainFrame)
                    {
                        resetEvent.Set();
                    }
                });
                browser.FinishLoadingFrameEvent += listener;
                try
                {
                    browser.LoadURL("teamdev.com/dotnetbrowser");
                    resetEvent.WaitOne(new TimeSpan(0, 0, 45));
                }
                finally
                {
                    browser.FinishLoadingFrameEvent -= listener;
                }

                // #3 Set the required document size.
                JSValue documentHeight = browserView.Browser.ExecuteJavaScriptAndReturnValue(
                    "Math.max(document.body.scrollHeight, " +
                    "document.documentElement.scrollHeight, document.body.offsetHeight, " +
                    "document.documentElement.offsetHeight, document.body.clientHeight, " +
                    "document.documentElement.clientHeight);");
                JSValue documentWidth = browserView.Browser.ExecuteJavaScriptAndReturnValue(
                    "Math.max(document.body.scrollWidth, " +
                    "document.documentElement.scrollWidth, document.body.offsetWidth, " +
                    "document.documentElement.offsetWidth, document.body.clientWidth, " +
                    "document.documentElement.clientWidth);");

                int scrollBarSize = 25;

                int viewWidth = (int)documentWidth.GetNumber() + scrollBarSize;
                viewHeight = (int)documentHeight.GetNumber() + scrollBarSize;
                var viewSize = new System.Drawing.Size(viewWidth, viewHeight);

                // #4 Register OnRepaint to get notifications
                // about paint events. We expect that web page will be completely rendered twice:
                // 1. When its size is updated.
                // 2. When HTML content is loaded and displayed.
                DrawingView drawingView = (DrawingView)browserView.GetInnerView();
                drawingView.OnRedraw += delegate(object sender, OnRedrawEventArgs e)
                {
                    // Make sure that all view content has been repainted.
                    if (e.UpdatedRect.Size.Equals(viewSize))
                    {
                        browserView.GetImage().Save(@"teamdev.png", ImageFormat.Png);
                        Application.Current.Shutdown();
                    }
                };
                browserView.Browser.SetSize(viewWidth, viewHeight);
            };
        }