{"record":{"id":"5ae7f4d23ebf9e1e","repo":"AvaloniaUI/Avalonia","slug":"only-ibitmap-supported-as-source","errorCode":null,"errorMessage":"Only IBitmap supported as source","messagePattern":"Only IBitmap supported as source","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Media/Imaging/CroppedBitmap.cs","lineNumber":64,"sourceCode":"            set => SetValue(SourceRectProperty, value);\n        }\n\n        public CroppedBitmap()\n        {\n        }\n\n        public CroppedBitmap(IImage source, PixelRect sourceRect)\n        {\n            Source = source;\n            SourceRect = sourceRect;\n        }\n\n        private void SourceChanged(AvaloniaPropertyChangedEventArgs e)\n        {\n            if (e.NewValue == null)\n                return;\n            if (!(e.NewValue is IBitmap))\n                throw new ArgumentException(\"Only IBitmap supported as source\");\n            Invalidated?.Invoke(this, e);\n        }\n\n        private void SourceRectChanged(AvaloniaPropertyChangedEventArgs e) => Invalidated?.Invoke(this, e);\n\n        public virtual void Dispose()\n        {\n            (Source as IBitmap)?.Dispose();\n        }\n\n        public Size Size {\n            get\n            {\n                if (Source is not IBitmap bmp)\n                    return default;\n                if (SourceRect.Width == 0 && SourceRect.Height == 0)\n                    return Source.Size;\n                return SourceRect.Size.ToSizeWithDpi(bmp.Dpi);","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Media/Imaging/CroppedBitmap.cs#L46-L82","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Set Source to a Bitmap/WriteableBitmap (IBitmap) instance.","If you only have a vector IImage, render it to a WriteableBitmap first, then crop the result.","Validate the binding source type; change the property type to IBitmap to make the contract explicit."],"exampleFix":"// before\ncrop.Source = drawingImage; // IImage, not IBitmap -> throws\n\n// after\nusing var rtb = new RenderTargetBitmap(pixelSize);\nusing (var ctx = rtb.CreateDrawingContext())\n    drawingImage.Draw(ctx, new Rect(drawingImage.Size));\ncrop.Source = rtb; // IBitmap","handlingStrategy":"type-guard","validationCode":"if (image is not IBitmap)\n    throw new ArgumentException(\"CroppedBitmap.Source must be an IBitmap (raster).\", nameof(image));\ncrop.Source = (IBitmap)image;","typeGuard":"static bool IsCropSource(IImage img) => img is IBitmap;","tryCatchPattern":null,"preventionTips":["Type the Source property/binding as IBitmap, not IImage.","Rasterize vector IImage sources before assigning to CroppedBitmap.","Validate the source type at the binding boundary."],"tags":["imaging","croppedbitmap","type-mismatch"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}