AvaloniaUI/Avalonia · error · InvalidOperationException

{resource.GetType().FullName} can not be used with this Draw

Error message

{resource.GetType().FullName} can not be used with this DrawingContext

What it means

RenderDataDrawingContext.AddResource accepts only a closed set of resource types: null, immutable brushes/pens/transforms, CompositionBrush, or any ICompositionRenderResource. Anything else throws InvalidOperationException stating the type 'can not be used with this DrawingContext'. The context records resources into a composition render-data stream, so it must be able to map each resource to a server object.

Source

Thrown at src/Avalonia.Base/Rendering/Composition/Drawing/RenderDataDrawingContext.cs:63

        if (resource == null
            || resource is IImmutableBrush
            || resource is ImmutablePen
            || resource is ImmutableTransform
            || resource is CompositionBrush)
            return;

        if (resource is ICompositionRenderResource renderResource)
        {
            _resourcesHashSet ??= s_hashSetPool.Get();
            if (!_resourcesHashSet.Add(renderResource))
                return;

            renderResource.AddRefOnCompositor(_compositor);
            RenderData.AddResource(renderResource);
            return;
        }

        throw new InvalidOperationException(resource.GetType().FullName + " can not be used with this DrawingContext");
    }

    private void PushedScope(int positionBefore) =>
        (_pushStack ??= s_pushStackPool.Get()).Push(new PushEntry
        {
            Emitted = true,
            PositionBefore = positionBefore,
            PositionAfter = Stream.OpcodeLength,
            DepthBefore = Stream.Depth - 1
        });

    private void PushedNoOpScope() =>
        (_pushStack ??= s_pushStackPool.Get()).Push(new PushEntry { Emitted = false });

    private void PopCore()
    {
        var entry = _pushStack!.Pop();
        if (!entry.Emitted)

View on GitHub (pinned to 11c5427268)

Solutions

  1. Pass immutable variants (ImmutablePen, IImmutableBrush) or composition-backed resources (ICompositionRenderResource) to the drawing context.
  2. Convert custom brushes/pens to immutable or composition-backed equivalents before drawing.
  3. If you author custom resources, implement ICompositionRenderResource so AddResource can register them.

Example fix

// before
dc.DrawRectangle(myMutableBrush, myCustomPen, rect); // neither immutable nor composition -> throws

// after
var brush = new ImmutableSolidColorBrush(myMutableBrush.Color, myMutableBrush.Opacity);
var pen = new ImmutablePen(brush, myCustomPen.Thickness);
dc.DrawRectangle(brush, pen, rect);
Defensive patterns

Strategy: type-guard

Validate before calling

static bool IsAcceptedByDrawingContext(object? r) =>
    r is null or IImmutableBrush or ImmutablePen or ImmutableTransform or CompositionBrush or ICompositionRenderResource;

Type guard

static bool IsCompositionDrawableResource(object? r) =>
    r is ICompositionRenderResource or IImmutableBrush or ImmutablePen or ImmutableTransform or CompositionBrush;

Prevention

When it happens

Trigger: Drawing into a composition-backed DrawingContext with a mutable/non-immutable IBrush, IPen, IGeometry, or other resource that is not an ICompositionRenderResource and not an immutable variant.

Common situations: Passing a custom mutable brush/pen implementation into a composition drawing context; using a Geometry subclass that is neither immutable nor a composition resource.

Related errors


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