AvaloniaUI/Avalonia · error · ArgumentException

Only IBitmap supported as source

Error message

Only IBitmap supported as source

What it means

CroppedBitmap requires its Source to be an IBitmap (raster). The SourceChanged property-change handler throws ArgumentException if the new value is any IImage that is not IBitmap, because cropping is defined on raster pixel rectangles.

Source

Thrown at src/Avalonia.Base/Media/Imaging/CroppedBitmap.cs:64

            set => SetValue(SourceRectProperty, value);
        }

        public CroppedBitmap()
        {
        }

        public CroppedBitmap(IImage source, PixelRect sourceRect)
        {
            Source = source;
            SourceRect = sourceRect;
        }

        private void SourceChanged(AvaloniaPropertyChangedEventArgs e)
        {
            if (e.NewValue == null)
                return;
            if (!(e.NewValue is IBitmap))
                throw new ArgumentException("Only IBitmap supported as source");
            Invalidated?.Invoke(this, e);
        }

        private void SourceRectChanged(AvaloniaPropertyChangedEventArgs e) => Invalidated?.Invoke(this, e);

        public virtual void Dispose()
        {
            (Source as IBitmap)?.Dispose();
        }

        public Size Size {
            get
            {
                if (Source is not IBitmap bmp)
                    return default;
                if (SourceRect.Width == 0 && SourceRect.Height == 0)
                    return Source.Size;
                return SourceRect.Size.ToSizeWithDpi(bmp.Dpi);

View on GitHub (pinned to 11c5427268)

Solutions

  1. Set Source to a Bitmap/WriteableBitmap (IBitmap) instance.
  2. If you only have a vector IImage, render it to a WriteableBitmap first, then crop the result.
  3. Validate the binding source type; change the property type to IBitmap to make the contract explicit.

Example fix

// before
crop.Source = drawingImage; // IImage, not IBitmap -> throws

// after
using var rtb = new RenderTargetBitmap(pixelSize);
using (var ctx = rtb.CreateDrawingContext())
    drawingImage.Draw(ctx, new Rect(drawingImage.Size));
crop.Source = rtb; // IBitmap
Defensive patterns

Strategy: type-guard

Validate before calling

if (image is not IBitmap)
    throw new ArgumentException("CroppedBitmap.Source must be an IBitmap (raster).", nameof(image));
crop.Source = (IBitmap)image;

Type guard

static bool IsCropSource(IImage img) => img is IBitmap;

Prevention

When it happens

Trigger: Assigning a non-raster IImage to CroppedBitmap.Source — e.g. a DrawingImage, a RenderTargetBitmap-backed custom IImage, or any class implementing IImage but not IBitmap. Triggered when the Source StyledProperty change fires.

Common situations: Pointing CroppedBitmap at a vector/DrawingImage; binding Source to an IImage-typed property whose runtime value is not IBitmap; swapping in a custom IImage implementation.

Related errors


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