3

I have downloaded example of Minimalexample.Offscreen. This is the code I'm using for screenshot but I'm not getting the full page. The image is cropped (only visible page screenshot is taken).

//   c# code
 var scriptTask = browser.EvaluateScriptAsync("document.getElementById('lst-ib').value = 'CefSharp Was Here!'");
        scriptTask.ContinueWith(t =>
                {
                    Thread.Sleep(500);
                    var task = browser.ScreenshotAsync();
                    task.ContinueWith(x =>
                    {
                        var screenshotPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "CefSharp screenshot.png");

                        Console.WriteLine();
                        Console.WriteLine("Screenshot ready. Saving to {0}", screenshotPath);
                        task.Result.Save(screenshotPath);
                        task.Result.Dispose();
                        Console.WriteLine("Screenshot saved.  Launching your default image viewer...");                       
                        Process.Start(screenshotPath);
                        Console.WriteLine("Image viewer launched.  Press any key to exit.");            
                    }, TaskScheduler.Default);
                  }).Wait();

How can I get the full long page screenshot with CefSharp offscreen or Cefsharp winforms?

halfer
  • 19,824
  • 17
  • 99
  • 186
skhurams
  • 2,133
  • 7
  • 45
  • 82
  • Why don't you use `async/await`? There is no supported .NET version that has Task but not `async/await` (hint). This would make your code both cleaner and avoid leaks due to missed `Dispose` calls. As [this discussion shows](https://github.com/cefsharp/CefSharp/issues/957) your page may not have loaded fully, so `ScreenshotAsync` only captured the first frame – Panagiotis Kanavos Sep 09 '16 at 12:59
  • can you give any demo code example there is none out there and there is no other function for screenshot only screenshotasync is available – skhurams Sep 09 '16 at 13:08
  • how about now i have updated my code and added wait but still the same problem – skhurams Sep 09 '16 at 13:46
  • //i have also run this code var bitmap =browser.ScreenshotOrNull();bitmap.Save(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "ScreenPage.png")); // but still receving croped image – skhurams Sep 09 '16 at 14:14

2 Answers2

3

After long period of time i found the solution. The main point is to set webview height according to page height and then take screenshot.

CefSharp.OffScreen.ChromiumWebBrowser WebView =new CefSharp.OffScreen.ChromiumWebBrowser(siteUrl);
            
            int width = 1280;
            int height = 1480;

            string jsString = "Math.max(document.body.scrollHeight, " +
                              "document.documentElement.scrollHeight, document.body.offsetHeight, " +
                              "document.documentElement.offsetHeight, document.body.clientHeight, " +
                              "document.documentElement.clientHeight);";


            var executedScript = WebView.EvaluateScriptAsync(jsString).Result.Result;

            height = Convert.ToInt32(executedScript);

            var size = new Size(width, height);

            WebView.Size = size;

            Thread.Sleep(500);
            // Wait for the screenshot to be taken.
            var bitmap = WebView.ScreenshotOrNull();
            bitmap.Save(@"Test.jpg");
skhurams
  • 2,133
  • 7
  • 45
  • 82
2

May be because you are waiting only half a second for the page to load. See how much time it takes to load on Google Chrome and give those many seconds. For three seconds add this

Thread.Sleep(3000);
halfer
  • 19,824
  • 17
  • 99
  • 186
saquib adil
  • 146
  • 10