{"record":{"id":"6c820b37f3628bcc","repo":"AvaloniaUI/Avalonia","slug":"stride-6c820b","errorCode":null,"errorMessage":"stride","messagePattern":"stride","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Media/Imaging/WriteableBitmap.cs","lineNumber":55,"sourceCode":"            _pixelFormatMemory = pixelFormatMemory;\n        }\n\n        /// <summary>\n        /// Initializes a new instance of the <see cref=\"WriteableBitmap\"/> class with existing pixel data\n        /// The data is copied to the bitmap\n        /// </summary>\n        /// <param name=\"format\">The pixel format.</param>\n        /// <param name=\"alphaFormat\">The alpha format.</param>\n        /// <param name=\"data\">The pointer to the source bytes.</param>\n        /// <param name=\"size\">The size of the bitmap in device pixels.</param>\n        /// <param name=\"dpi\">The DPI of the bitmap.</param>\n        /// <param name=\"stride\">The number of bytes per row.</param>\n        public unsafe WriteableBitmap(PixelFormat format, AlphaFormat alphaFormat, IntPtr data, PixelSize size, Vector dpi, int stride)\n            : this(size, dpi, format, alphaFormat)\n        {\n            var minStride = (format.BitsPerPixel * size.Width + 7) / 8;\n            if (minStride > stride)\n                throw new ArgumentOutOfRangeException(nameof(stride));\n\n            using (var locked = Lock())\n            {\n                for (var y = 0; y < size.Height; y++)\n                    Unsafe.CopyBlock((locked.Address + locked.RowBytes * y).ToPointer(),\n                        (data + y * stride).ToPointer(), (uint)minStride);\n            }\n        }\n\n        public override PixelFormat? Format => _pixelFormatMemory?.Format ?? base.Format;\n        \n        public ILockedFramebuffer Lock()\n        {\n            if (_pixelFormatMemory == null)\n                return ((IWriteableBitmapImpl)PlatformImpl.Item).Lock();\n            \n            return new LockedFramebuffer(_pixelFormatMemory.Address, _pixelFormatMemory.Size,\n                _pixelFormatMemory.RowBytes,","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Media/Imaging/WriteableBitmap.cs#L37-L73","documentation":"The WriteableBitmap(IntPtr data) constructor computes a minimum per-row stride = (format.BitsPerPixel * width + 7) / 8 and throws ArgumentOutOfRangeException(nameof(stride)) when the supplied stride is smaller than that minimum. The stride must accommodate at least one full row of source pixels so the per-row Unsafe.CopyBlock copies valid data.","triggerScenarios":"Calling new WriteableBitmap(format, alphaFormat, data, size, dpi, stride) with a stride smaller than bitsPerPixel*width rounded up to bytes — e.g. passing width in bytes instead of the row pitch, or miscomputing bitsPerPixel for packed formats like BlackWhite/Gray2.","commonSituations":"Assuming stride = width (bytes) for a 32-bpp format; using width*sizeof(int) for a 16-bpp format; interop with a source buffer whose real pitch is larger but the caller passed the wrong value.","solutions":["Compute stride from the format: stride = (format.BitsPerPixel * size.Width + 7) / 8, or larger.","If the source buffer uses extra padding per row, pass the actual source row pitch and let Avalonia copy minStride bytes per row.","Validate stride >= minStride before constructing."],"exampleFix":"// before\nint stride = size.Width; // wrong for 32-bpp\nnew WriteableBitmap(PixelFormat.Bgra8888, AlphaFormat.Premultiplied, data, size, dpi, stride);\n\n// after\nint stride = (PixelFormat.Bgra8888.BitsPerPixel * size.Width + 7) / 8; // 4*width\nnew WriteableBitmap(PixelFormat.Bgra8888, AlphaFormat.Premultiplied, data, size, dpi, stride);","handlingStrategy":"validation","validationCode":"int minStride = (format.BitsPerPixel * size.Width + 7) / 8;\nif (stride < minStride)\n    throw new ArgumentOutOfRangeException(nameof(stride), $\"stride must be >= {minStride}.\");\nvar wb = new WriteableBitmap(format, alphaFormat, data, size, dpi, stride);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always compute stride from BitsPerPixel and width.","Account for packed formats (sub-byte-per-pixel) with the (bits+7)/8 rounding.","Pass the source buffer's real row pitch, not width."],"tags":["imaging","writeablebitmap","argument","stride"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}