{"record":{"id":"694b7d6d237cabea","repo":"AvaloniaUI/Avalonia","slug":"sourcerect","errorCode":null,"errorMessage":"sourceRect","messagePattern":"sourceRect","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Media/Imaging/Bitmap.cs","lineNumber":205,"sourceCode":"        public void Save(Stream stream, int? quality = null)\n        {\n            PlatformImpl.Item.Save(stream, PngBitmapEncoderOptions.Default);\n        }\n\n        /// <inheritdoc />\n        public void Save(Stream stream, BitmapEncoderOptions options)\n        {\n            PlatformImpl.Item.Save(stream, options);\n        }\n\n        public virtual PixelFormat? Format => (PlatformImpl.Item as IReadableBitmapImpl)?.Format;\n\n        public virtual AlphaFormat? AlphaFormat => (PlatformImpl.Item as IReadableBitmapImpl)?.AlphaFormat;\n\n        private PixelRect ValidateSourceRect(PixelRect sourceRect)\n        {\n            if ((sourceRect.Width <= 0 || sourceRect.Height <= 0) && (sourceRect.X != 0 || sourceRect.Y != 0))\n                throw new ArgumentOutOfRangeException(nameof(sourceRect));\n\n            if (sourceRect.X < 0 || sourceRect.Y < 0)\n                throw new ArgumentOutOfRangeException(nameof(sourceRect));\n\n            if (sourceRect.Width <= 0)\n                sourceRect = sourceRect.WithWidth(PixelSize.Width);\n            if (sourceRect.Height <= 0)\n                sourceRect = sourceRect.WithHeight(PixelSize.Height);\n\n            if (sourceRect.Right > PixelSize.Width || sourceRect.Bottom > PixelSize.Height)\n                throw new ArgumentOutOfRangeException(nameof(sourceRect));\n            return sourceRect;\n        }\n        \n        /// <summary>\n        /// Performs a row-by-row copy of pixels from a source buffer into a destination buffer.\n        /// </summary>\n        /// <remarks>","sourceCodeStart":187,"sourceCodeEnd":223,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Media/Imaging/Bitmap.cs#L187-L223","documentation":"Bitmap.ValidateSourceRect's first guard rejects a sourceRect that has non-positive Width or Height AND a non-zero X or Y. The intent: zero width/height is treated as 'default to full bitmap' only when X=Y=0; a rect with both a non-zero offset and a zero extent is meaningless.","triggerScenarios":"Calling CopyPixels/Save with a sourceRect = new PixelRect(x, y, 0, h) or (x, y, w, 0) where x or y != 0; passing a source rectangle computed from a selection whose width/height collapsed but whose origin moved.","commonSituations":"A cropping UI where the user dragged the selection to zero width but kept it off the origin; binding sourceRect fields from sliders that can individually zero out; computing a sub-rect from a layout that yields a zero dimension with an offset.","solutions":["If the intent is 'no sub-rect', pass PixelRect.Empty or new PixelRect(0,0,0,0) so the defaulting branch applies.","Guard before the call: if (rect.Width <= 0 || rect.Height <= 0) rect = new PixelRect(rect.X, rect.Y, bmp.PixelSize.Width - rect.X, bmp.PixelSize.Height - rect.Y);","Clamp selection rectangles so X=0 when width is zero, or never let width/height reach zero with a non-zero origin.","Add an assertion in your crop logic to surface zero-extent off-origin rectangles early."],"exampleFix":"// before\nbmp.CopyPixels(new PixelRect(10, 10, 0, 0), ...); // throws\n\n// after\nvar r = new PixelRect(10, 10, 0, 0);\nif (r.Width <= 0 || r.Height <= 0)\n    r = new PixelRect(0, 0, 0, 0); // signal 'use whole bitmap'\nbmp.CopyPixels(r, ...);","handlingStrategy":"validation","validationCode":"static PixelRect NormalizeSourceRect(PixelRect r, PixelSize bmp)\n{\n    if (r.Width <= 0 || r.Height <= 0)\n    {\n        if (r.X != 0 || r.Y != 0)\n            r = new PixelRect(0, 0, 0, 0); // signal 'whole bitmap'\n    }\n    return r;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass PixelRect.Empty (0,0,0,0) to mean 'whole bitmap'.","Never let a selection rect have a non-zero origin with a zero extent.","Add a debug assertion for off-origin zero-extent rectangles."],"tags":["imaging","bitmap","source-rect","bounds","argument-validation"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}