cefsharp/CefSharp · error · Exception

Invalid size, must be greater than 0,0

Error message

Invalid size, must be greater than 0,0

What it means

CreateBrowser explicitly creates the underlying offscreen CEF browser before the control is attached to the visual tree. CEF requires a non-zero initial size to allocate its backing surface. Passing Size.Empty or a size equal to (0, 0) is rejected because a zero-area browser window is invalid.

Source

Thrown at CefSharp.Wpf/ChromiumWebBrowser.cs:1977

        {
            //We maintain a manual reference to the controls screen location
            //(relative to top/left of the screen)
            browserScreenLocation = GetBrowserScreenLocation();
        }

        /// <summary>
        /// Create the underlying CefBrowser instance with the specified <paramref name="initialSize"/>.
        /// This method should only be used in instances where you need the browser
        /// to load before it's attached to the Visual Tree. 
        /// </summary>
        /// <param name="parentWindowHwndSource">HwndSource for the Window that will host the browser.</param>
        /// <param name="initialSize">initial size</param>
        /// <returns>Returns false if browser already created, otherwise true.</returns>
        public bool CreateBrowser(HwndSource parentWindowHwndSource, Size initialSize)
        {
            if (initialSize.IsEmpty || initialSize.Equals(new Size(0, 0)))
            {
                throw new Exception("Invalid size, must be greater than 0,0");
            }

            source = parentWindowHwndSource;

            if (!CreateOffscreenBrowser(initialSize))
            {
                return false;
            }

            RoutedEventHandler handler = null;
            handler = (sender, args) =>
            {
                Loaded -= handler;

                //If the browser has finished rendering before we attach to the Visual Tree
                //then ask for a new frame to be generated
                var host = this.GetBrowserHost();
                if (host != null)

View on GitHub (pinned to 16bc6e0711)

Solutions

  1. Defer the CreateBrowser call until after the host window has completed its layout pass — use ContentRendered or SizeChanged with non-zero dimensions.
  2. Pass an explicit positive size, e.g. new Size(800, 600), instead of relying on measured values that may still be zero.
  3. Check initialSize.Width > 0 && initialSize.Height > 0 before calling CreateBrowser.

Example fix

// before
browser.CreateBrowser(source, new Size(window.ActualWidth, window.ActualHeight));

// after — guard with a fallback
var w = window.ActualWidth > 0 ? window.ActualWidth : 800;
var h = window.ActualHeight > 0 ? window.ActualHeight : 600;
browser.CreateBrowser(source, new Size(w, h));
Defensive patterns

Strategy: validation

Validate before calling

if (initialSize.IsEmpty || initialSize.Width <= 0 || initialSize.Height <= 0)
{
    throw new ArgumentException(
        $"initialSize must be positive, got {initialSize}",
        nameof(initialSize));
}
// safe to call now
browser.CreateBrowser(source, initialSize);

Prevention

When it happens

Trigger: Calling CreateBrowser(parentWindowHwndSource, initialSize) where initialSize is new Size(0,0), default(Size), or a Size whose Width or Height is zero. Common when the host window has not yet been measured/arranged.

Common situations: Invoking CreateBrowser inside a Window constructor or an early Loaded handler before the layout pass has assigned actual dimensions. Computing size from ActualWidth/ActualHeight that are still 0 at call time.

Related errors


AI-assisted analysis of cefsharp/CefSharp@16bc6e0711 (2026-08-13). Data as JSON: /api/errors/3182209b68018601. Report an issue: GitHub.