{"record":{"id":"94ce1e07cc63f289","repo":"cefsharp/CefSharp","slug":"unable-to-take-screenshot-while-browser-is-loading","errorCode":null,"errorMessage":"Unable to take screenshot while browser is loading","messagePattern":"Unable to take screenshot while browser is loading","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"CefSharp.Wpf.Example/Controls/ChromiumWebBrowserWithScreenshotSupport.cs","lineNumber":61,"sourceCode":"        {\n            ScreenshotCommand = new RelayCommand(TakeScreenshot);\n        }\n\n        public Task<InteropBitmap> TakeScreenshot(Size screenshotSize, int? frameRate = 1, int? ignoreFrames = 0, CancellationToken? cancellationToken = null)\n        {\n            if (screenshotTaskCompletionSource != null && screenshotTaskCompletionSource.Task.Status == TaskStatus.Running)\n            {\n                throw new Exception(\"Screenshot already in progress, you must wait for the previous screenshot to complete\");\n            }\n\n            if (IsBrowserInitialized == false)\n            {\n                throw new Exception(\"Browser has not yet finished initializing or is being disposed\");\n            }\n\n            if (IsLoading)\n            {\n                throw new Exception(\"Unable to take screenshot while browser is loading\");\n            }\n\n            var browserHost = this.GetBrowserHost();\n\n            if (browserHost == null)\n            {\n                throw new Exception(\"IBrowserHost is null\");\n            }\n\n            screenshotTaskCompletionSource = new TaskCompletionSource<InteropBitmap>(TaskCreationOptions.RunContinuationsAsynchronously);\n\n            if (cancellationToken.HasValue)\n            {\n                var token = cancellationToken.Value;\n                cancellationTokenRegistration = token.Register(() =>\n                {\n                    screenshotTaskCompletionSource.TrySetCanceled();\n","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/cefsharp/CefSharp/blob/16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec/CefSharp.Wpf.Example/Controls/ChromiumWebBrowserWithScreenshotSupport.cs#L43-L79","documentation":"Thrown by the example ChromiumWebBrowserWithScreenshotSupport.TakeScreenshot when IsLoading is true. Capturing mid-navigation would yield a partial or transitional frame, so the method requires the page to be in a stable, loaded state. This is a guard sequenced after the initialization and concurrency checks.","triggerScenarios":"Calling TakeScreenshot while a page is still loading (between LoadStart and LoadEnd/LoadingStateChanged idle); capturing immediately after Load(url) without waiting for completion.","commonSituations":"Screenshotting right after navigation without awaiting LoadingStateChanged; capturing during resource-heavy page loads that take seconds.","solutions":["Wait until LoadingStateChanged reports IsLoading == false before capturing.","Use WaitForNavigationAsync / LoadUrlAsync and await completion, then capture.","Guard with `if (!browser.IsLoading)` and retry when idle.","For animated pages, account for the frame-rate/ignoreFrames parameters once idle."],"exampleFix":"// before\nbrowser.Load(url);\nvar shot = browser.TakeScreenshot(size); // throws: still loading\n\n// after\nawait browser.LoadUrlAsync(url); // waits for load\nvar shot = await browser.TakeScreenshot(size);","handlingStrategy":"validation","validationCode":"if (!browser.IsLoading)\n{\n    _ = browser.TakeScreenshot(size);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Await LoadUrlAsync / WaitForNavigationAsync before capturing.","Watch LoadingStateChanged for IsLoading == false.","Retry capture when the page reaches idle."],"tags":["cefwsharp","wpf","screenshot","navigation"],"backgroundTag":null,"analyzedSha":"16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec","analyzedAt":"2026-08-13T19:57:05.828Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}