Example #1
1
        private static async void MainAsync(string cachePath, double zoomLevel)
        {
            var browserSettings = new BrowserSettings();

            //Reduce rendering speed to one frame per second so it's easier to take screen shots
            browserSettings.WindowlessFrameRate = 1;
            var requestContextSettings = new RequestContextSettings {
                CachePath = cachePath
            };

            // RequestContext can be shared between browser instances and allows for custom settings
            // e.g. CachePath
            using (var requestContext = new RequestContext(requestContextSettings))
                using (var browser = new ChromiumWebBrowser(TestUrl, browserSettings, requestContext))
                {
                    if (zoomLevel > 1)
                    {
                        browser.FrameLoadStart += (s, argsi) =>
                        {
                            var b = (ChromiumWebBrowser)s;
                            if (argsi.Frame.IsMain)
                            {
                                b.SetZoomLevel(zoomLevel);
                            }
                        };
                    }
                    await LoadPageAsync(browser);

                    var preferences = requestContext.GetAllPreferences(true);

                    // For Google.com pre-pupulate the search text box
                    await browser.EvaluateScriptAsync("document.getElementById('lst-ib').value = 'CefSharp Was Here!'");

                    // Wait for the screenshot to be taken,
                    // if one exists ignore it, wait for a new one to make sure we have the most up to date
                    await browser.ScreenshotAsync(true).ContinueWith(DisplayBitmap);

                    await LoadPageAsync(browser, "http://github.com");


                    //Gets a wrapper around the underlying CefBrowser instance
                    var cefBrowser = browser.GetBrowser();
                    // Gets a warpper around the CefBrowserHost instance
                    // You can perform a lot of low level browser operations using this interface
                    var cefHost = cefBrowser.GetHost();

                    //You can call Invalidate to redraw/refresh the image
                    cefHost.Invalidate(PaintElementType.View);

                    // Wait for the screenshot to be taken.
                    await browser.ScreenshotAsync(true).ContinueWith(DisplayBitmap);
                }
        }
Example #2
0
        private static async void MainAsync(string cachePath, double zoomLevel)
        {
            var browserSettings = new BrowserSettings();
            //Reduce rendering speed to one frame per second so it's easier to take screen shots
            browserSettings.WindowlessFrameRate = 1;
            var requestContextSettings = new RequestContextSettings { CachePath = cachePath };

            // RequestContext can be shared between browser instances and allows for custom settings
            // e.g. CachePath
            using(var requestContext = new RequestContext(requestContextSettings))
            using (var browser = new ChromiumWebBrowser(TestUrl, browserSettings, requestContext))
            {
                if (zoomLevel > 1)
                {
                    browser.FrameLoadStart += (s, argsi) =>
                    {
                        var b = (ChromiumWebBrowser)s;
                        if (argsi.Frame.IsMain)
                        {
                            b.SetZoomLevel(zoomLevel);
                        }
                    };
                }
                await LoadPageAsync(browser);

                var preferences = requestContext.GetAllPreferences(true);

                // For Google.com pre-pupulate the search text box
                await browser.EvaluateScriptAsync("document.getElementById('lst-ib').value = 'CefSharp Was Here!'");

                // Wait for the screenshot to be taken,
                // if one exists ignore it, wait for a new one to make sure we have the most up to date
                await browser.ScreenshotAsync(true).ContinueWith(DisplayBitmap);

                await LoadPageAsync(browser, "http://github.com");

                
                //Gets a wrapper around the underlying CefBrowser instance
                var cefBrowser = browser.GetBrowser();
                // Gets a warpper around the CefBrowserHost instance
                // You can perform a lot of low level browser operations using this interface
                var cefHost = cefBrowser.GetHost();

                //You can call Invalidate to redraw/refresh the image
                cefHost.Invalidate(PaintElementType.View);

                // Wait for the screenshot to be taken.
                await browser.ScreenshotAsync(true).ContinueWith(DisplayBitmap);
            }
        }
Example #3
0
        public async Task MainAsync(GraphicsDevice gd, IntPtr windowHandle, string url, object data, System.Drawing.Size size, double zoomLevel = 1.0)
        {
            if (Browser != null)
            {
                Browser.NewFrame -= Browser_NewFrame;
                Browser.Dispose();
            }

            var browserSettings = new BrowserSettings();

            //Reduce rendering speed to one frame per second so it's easier to take screen shots
            browserSettings.WindowlessFrameRate = 30;
            var requestContextSettings = new RequestContextSettings {
                CachePath = CachePath
            };

            // RequestContext can be shared between browser instances and allows for custom settings
            // e.g. CachePath
            RequestContext = new RequestContext(requestContextSettings);
            Browser        = new MonoCefBrowser(gd, url, browserSettings, RequestContext);
            Browser.CreateBrowser(new WindowInfo()
            {
                WindowHandle = windowHandle,
                Width        = size.Width,
                Height       = size.Height,
                WindowlessRenderingEnabled = true
            }, browserSettings);
            Browser.NewFrame += Browser_NewFrame;
            Browser.Size      = size;
            if (zoomLevel > 1)
            {
                Browser.FrameLoadStart += (s, argsi) =>
                {
                    var b = (ChromiumWebBrowser)s;
                    if (argsi.Frame.IsMain)
                    {
                        b.SetZoomLevel(zoomLevel);
                    }
                };
            }
            await LoadPageAsync(Browser);

            //Check preferences on the CEF UI Thread
            await Cef.UIThreadTaskFactory.StartNew(delegate
            {
                var preferences = RequestContext.GetAllPreferences(true);

                //Check do not track status
                var doNotTrack = (bool)preferences["enable_do_not_track"];

                Debug.WriteLine("DoNotTrack:" + doNotTrack);
            });

            var onUi = Cef.CurrentlyOnThread(CefThreadIds.TID_UI);

            await LoadPageAsync(Browser, url);

            //Gets a wrapper around the underlying CefBrowser instance
            var cefBrowser = Browser.GetBrowser();
            // Gets a warpper around the CefBrowserHost instance
            // You can perform a lot of low level browser operations using this interface
            var cefHost = cefBrowser.GetHost();

            cefHost.SendFocusEvent(true);

            SetMarshalledData(data);

            //You can call Invalidate to redraw/refresh the image
            cefHost.Invalidate(PaintElementType.View);
        }
Example #4
0
        private static async void MainAsync(string cachePath, double zoomLevel)
        {
            var browserSettings = new BrowserSettings();

            //Reduce rendering speed to one frame per second so it's easier to take screen shots
            browserSettings.WindowlessFrameRate = 1;
            var requestContextSettings = new RequestContextSettings {
                CachePath = cachePath
            };

            // RequestContext can be shared between browser instances and allows for custom settings
            // e.g. CachePath
            using (var requestContext = new RequestContext(requestContextSettings))
                using (var browser = new ChromiumWebBrowser(TestUrl, browserSettings, requestContext))
                {
                    if (zoomLevel > 1)
                    {
                        browser.FrameLoadStart += (s, argsi) =>
                        {
                            var b = (ChromiumWebBrowser)s;
                            if (argsi.Frame.IsMain)
                            {
                                b.SetZoomLevel(zoomLevel);
                            }
                        };
                    }
                    await LoadPageAsync(browser);

                    //Check preferences on the CEF UI Thread
                    await Cef.UIThreadTaskFactory.StartNew(delegate
                    {
                        var preferences = requestContext.GetAllPreferences(true);

                        //Check do not track status
                        var doNotTrack = (bool)preferences["enable_do_not_track"];

                        Debug.WriteLine("DoNotTrack: " + doNotTrack);
                    });

                    var onUi = Cef.CurrentlyOnThread(CefThreadIds.TID_UI);

                    // For Google.com pre-pupulate the search text box
                    await browser.EvaluateScriptAsync("document.getElementById('lst-ib').value = 'CefSharp Was Here!'");

                    //Example using SendKeyEvent for input instead of javascript
                    //var browserHost = browser.GetBrowserHost();
                    //var inputString = "CefSharp Was Here!";
                    //foreach(var c in inputString)
                    //{
                    //	browserHost.SendKeyEvent(new KeyEvent { WindowsKeyCode = c, Type = KeyEventType.Char });
                    //}

                    ////Give the browser a little time to finish drawing our SendKeyEvent input
                    //await Task.Delay(100);

                    // Wait for the screenshot to be taken,
                    // if one exists ignore it, wait for a new one to make sure we have the most up to date
                    await browser.ScreenshotAsync(true).ContinueWith(DisplayBitmap);

                    await LoadPageAsync(browser, "http://github.com");

                    //Gets a wrapper around the underlying CefBrowser instance
                    var cefBrowser = browser.GetBrowser();
                    // Gets a warpper around the CefBrowserHost instance
                    // You can perform a lot of low level browser operations using this interface
                    var cefHost = cefBrowser.GetHost();

                    //You can call Invalidate to redraw/refresh the image
                    cefHost.Invalidate(PaintElementType.View);

                    // Wait for the screenshot to be taken.
                    await browser.ScreenshotAsync(true).ContinueWith(DisplayBitmap);
                }
        }
Example #5
0
        private static async Task MainAsync(string url, string secondUrl, string cachePath, double zoomLevel)
        {
            var browserSettings = new BrowserSettings
            {
                //Reduce rendering speed to one frame per second so it's easier to take screen shots
                WindowlessFrameRate = 1
            };

            var requestContextSettings = new RequestContextSettings
            {
                CachePath = Path.GetFullPath(cachePath)
            };

            // RequestContext can be shared between browser instances and allows for custom settings
            // e.g. CachePath
            using (var requestContext = new RequestContext(requestContextSettings))
                using (var browser = new ChromiumWebBrowser(url, browserSettings, requestContext))
                {
                    if (zoomLevel > 1)
                    {
                        browser.FrameLoadStart += (s, argsi) =>
                        {
                            var b = (ChromiumWebBrowser)s;
                            if (argsi.Frame.IsMain)
                            {
                                b.SetZoomLevel(zoomLevel);
                            }
                        };
                    }
                    await browser.WaitForInitialLoadAsync();

                    //Check preferences on the CEF UI Thread
                    await Cef.UIThreadTaskFactory.StartNew(delegate
                    {
                        var preferences = requestContext.GetAllPreferences(true);

                        //Check do not track status
                        var doNotTrack = (bool)preferences["enable_do_not_track"];

                        Debug.WriteLine("DoNotTrack: " + doNotTrack);
                    });

                    var onUi = Cef.CurrentlyOnThread(CefThreadIds.TID_UI);

                    // For Google.com pre-pupulate the search text box
                    if (url.Contains("google.com"))
                    {
                        await browser.EvaluateScriptAsync("document.querySelector('[name=q]').value = 'CefSharp Was Here!'");
                    }

                    //Example using SendKeyEvent for input instead of javascript
                    //var browserHost = browser.GetBrowserHost();
                    //var inputString = "CefSharp Was Here!";
                    //foreach(var c in inputString)
                    //{
                    //	browserHost.SendKeyEvent(new KeyEvent { WindowsKeyCode = c, Type = KeyEventType.Char });
                    //}

                    ////Give the browser a little time to finish drawing our SendKeyEvent input
                    //await Task.Delay(100);

                    var contentSize = await browser.GetContentSizeAsync();

                    var viewport = new Viewport
                    {
                        Height = contentSize.Height,
                        Width  = contentSize.Width,
                        Scale  = 1.0
                    };

                    // Wait for the screenshot to be taken,
                    // if one exists ignore it, wait for a new one to make sure we have the most up to date
                    var bitmap = await browser.CaptureScreenshotAsync(viewport : viewport);

                    // Make a file to save it to (e.g. C:\Users\jan\Desktop\CefSharp screenshot.png)
                    var screenshotPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "CefSharp screenshot" + DateTime.Now.Ticks + ".png");

                    Console.WriteLine();
                    Console.WriteLine("Screenshot ready. Saving to {0}", screenshotPath);

                    File.WriteAllBytes(screenshotPath, bitmap);

                    Console.WriteLine("Screenshot saved. Launching your default image viewer...");

                    // Tell Windows to launch the saved image.
                    Process.Start(new ProcessStartInfo(screenshotPath)
                    {
                        // UseShellExecute is false by default on .NET Core.
                        UseShellExecute = true
                    });

                    await browser.LoadUrlAsync(secondUrl);

                    // Gets a warpper around the CefBrowserHost instance
                    // You can perform a lot of low level browser operations using this interface
                    var cefbrowserHost = browser.GetBrowserHost();

                    //You can call Invalidate to redraw/refresh the image
                    cefbrowserHost.Invalidate(PaintElementType.View);

                    contentSize = await browser.GetContentSizeAsync();

                    viewport = new Viewport
                    {
                        Height = contentSize.Height,
                        Width  = contentSize.Width,
                        Scale  = 1.0
                    };

                    // Wait for the screenshot to be taken,
                    // if one exists ignore it, wait for a new one to make sure we have the most up to date
                    bitmap = await browser.CaptureScreenshotAsync(viewport : viewport);

                    screenshotPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "CefSharp screenshot" + DateTime.Now.Ticks + ".png");

                    Console.WriteLine();
                    Console.WriteLine("Screenshot ready. Saving to {0}", screenshotPath);

                    File.WriteAllBytes(screenshotPath, bitmap);

                    Console.WriteLine("Screenshot saved. Launching your default image viewer...");

                    // Tell Windows to launch the saved image.
                    Process.Start(new ProcessStartInfo(screenshotPath)
                    {
                        // UseShellExecute is false by default on .NET Core.
                        UseShellExecute = true
                    });
                }
        }