cefsharp/CefSharp · error · Exception
Unable to take screenshot while browser is loading
Error message
Unable to take screenshot while browser is loading
What it means
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.
Source
Thrown at CefSharp.Wpf.Example/Controls/ChromiumWebBrowserWithScreenshotSupport.cs:61
{
ScreenshotCommand = new RelayCommand(TakeScreenshot);
}
public Task<InteropBitmap> TakeScreenshot(Size screenshotSize, int? frameRate = 1, int? ignoreFrames = 0, CancellationToken? cancellationToken = null)
{
if (screenshotTaskCompletionSource != null && screenshotTaskCompletionSource.Task.Status == TaskStatus.Running)
{
throw new Exception("Screenshot already in progress, you must wait for the previous screenshot to complete");
}
if (IsBrowserInitialized == false)
{
throw new Exception("Browser has not yet finished initializing or is being disposed");
}
if (IsLoading)
{
throw new Exception("Unable to take screenshot while browser is loading");
}
var browserHost = this.GetBrowserHost();
if (browserHost == null)
{
throw new Exception("IBrowserHost is null");
}
screenshotTaskCompletionSource = new TaskCompletionSource<InteropBitmap>(TaskCreationOptions.RunContinuationsAsynchronously);
if (cancellationToken.HasValue)
{
var token = cancellationToken.Value;
cancellationTokenRegistration = token.Register(() =>
{
screenshotTaskCompletionSource.TrySetCanceled();
View on GitHub (pinned to 16bc6e0711)
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.
Example fix
// before browser.Load(url); var shot = browser.TakeScreenshot(size); // throws: still loading // after await browser.LoadUrlAsync(url); // waits for load var shot = await browser.TakeScreenshot(size);
Defensive patterns
Strategy: validation
Validate before calling
if (!browser.IsLoading)
{
_ = browser.TakeScreenshot(size);
} Prevention
- Await LoadUrlAsync / WaitForNavigationAsync before capturing.
- Watch LoadingStateChanged for IsLoading == false.
- Retry capture when the page reaches idle.
When it happens
Trigger: Calling TakeScreenshot while a page is still loading (between LoadStart and LoadEnd/LoadingStateChanged idle); capturing immediately after Load(url) without waiting for completion.
Common situations: Screenshotting right after navigation without awaiting LoadingStateChanged; capturing during resource-heavy page loads that take seconds.
Related errors
- Screenshot already in progress, you must wait for the previo
- Browser has not yet finished initializing or is being dispos
- IBrowserHost is null
- IBrowser instance is no longer valid. Control.Handle was lik
- {nameof(viewPort)}.{nameof(viewPort.Scale)} must be greater
AI-assisted analysis of cefsharp/CefSharp@16bc6e0711 (2026-08-13).
Data as JSON: /api/errors/94ce1e07cc63f289.
Report an issue: GitHub.