AvaloniaUI/Avalonia · error · ArgumentNullException

source

Error message

source

What it means

DrawBitmap(Bitmap source, Rect rect) throws ArgumentNullException(nameof(source)) when source is null. The drawing context requires a concrete Bitmap to blit.

Source

Thrown at src/Avalonia.Base/Media/ImmediateDrawingContext.cs:77

        public Matrix CurrentTransform
        {
            get { return _currentTransform; }
            private set
            {
                _currentTransform = value;
                var transform = _currentTransform * _currentContainerTransform;
                PlatformImpl.Transform = transform;
            }
        }

        /// <summary>
        /// Draws an bitmap.
        /// </summary>
        /// <param name="source">The bitmap.</param>
        /// <param name="rect">The rect in the output to draw to.</param>
        public void DrawBitmap(Bitmap source, Rect rect)
        {
            _ = source ?? throw new ArgumentNullException(nameof(source));
            DrawBitmap(source, new Rect(source.Size), rect);
        }

        /// <summary>
        /// Draws an image.
        /// </summary>
        /// <param name="source">The bitmap.</param>
        /// <param name="sourceRect">The rect in the image to draw.</param>
        /// <param name="destRect">The rect in the output to draw to.</param>
        public void DrawBitmap(Bitmap source, Rect sourceRect, Rect destRect)
        {
            _ = source ?? throw new ArgumentNullException(nameof(source));
            PlatformImpl.DrawBitmap(source.PlatformImpl.Item, 1, sourceRect, destRect);
        }

        /// <summary>
        /// Draws a line.
        /// </summary>

View on GitHub (pinned to 11c5427268)

Solutions

  1. Null-check the bitmap before calling DrawBitmap.
  2. Ensure the image source is loaded/assigned before the render pass.
  3. Provide a fallback placeholder bitmap.

Example fix

// before
context.DrawBitmap(_image, rect); // _image may be null

// after
if (_image is not null)
    context.DrawBitmap(_image, rect);
Defensive patterns

Strategy: validation

Validate before calling

if (source is null) return;
context.DrawBitmap(source, rect);

Prevention

When it happens

Trigger: Calling drawingContext.DrawBitmap(null, rect) — e.g. when a bound image source is null or a field has not been initialized before render.

Common situations: Rendering in an OnRender override before the image is loaded; a null ImageBrush/source bound at runtime; a race where the source is cleared during a draw.

Related errors


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/c67d0b45fea1875d. Report an issue: GitHub.