{"record":{"id":"c016c9a7468d5ec9","repo":"AvaloniaUI/Avalonia","slug":"copypixels-is-not-supported-for-this-bitmap-type","errorCode":null,"errorMessage":"CopyPixels is not supported for this bitmap type","messagePattern":"CopyPixels is not supported for this bitmap type","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Media/Imaging/Bitmap.cs","lineNumber":285,"sourceCode":"        /// given framebuffer. Self-contained convenience wrapper around the static\n        /// <see cref=\"CopyPixelsCore(PixelRect,IntPtr,int,PixelFormat,IntPtr,int,int)\"/> for inheritors.\n        /// </summary>\n        private protected void CopyPixelsCore(PixelRect sourceRect, IntPtr buffer, int bufferSize, int stride,\n            ILockedFramebuffer fb)\n        {\n            sourceRect = ValidateSourceRect(sourceRect);\n            CopyPixelsCore(sourceRect, fb.Address, fb.RowBytes, fb.Format, buffer, bufferSize, stride);\n        }\n\n        public virtual void CopyPixels(PixelRect sourceRect, IntPtr buffer, int bufferSize, int stride)\n        {\n            if (\n                Format == null\n                || PlatformImpl.Item is not IReadableBitmapImpl readable\n                || Format != readable.Format\n            )\n            {\n                throw new NotSupportedException(\"CopyPixels is not supported for this bitmap type\");\n            }\n\n            if (_isTranscoded)\n                throw new NotSupportedException(\"CopyPixels is not supported for transcoded bitmaps\");\n\n            using (var fb = readable.Lock())\n                CopyPixelsCore(sourceRect, buffer, bufferSize, stride, fb);\n        }\n\n        /// <summary>\n        /// Copies pixels to the target buffer and transcodes the pixel and alpha format if needed.\n        /// </summary>\n        /// <param name=\"buffer\">The target buffer.</param>\n        /// <exception cref=\"NotSupportedException\"></exception>\n        public void CopyPixels(ILockedFramebuffer buffer)\n        {\n            if (PlatformImpl.Item is not IReadableBitmapImpl readable || readable.Format == null || readable.AlphaFormat == null)\n            {","sourceCodeStart":267,"sourceCodeEnd":303,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Media/Imaging/Bitmap.cs#L267-L303","documentation":"Thrown by the IntPtr-buffer CopyPixels overload when the bitmap cannot supply readable pixels in its declared format. The guard rejects three cases: Format is null (format-less/empty bitmap), the platform impl is not IReadableBitmapImpl (e.g. a GPU/deferred texture that cannot be locked for CPU reads), or the bitmap's Format differs from the readable impl's native Format. For transcoded or non-readable bitmaps you must go through the transcoding-aware CopyPixels(ILockedFramebuffer) overload instead.","triggerScenarios":"Calling bitmap.CopyPixels(sourceRect, buffer, bufferSize, stride) on a bitmap whose PlatformImpl.Item does not implement IReadableBitmapImpl, or whose Format is null, or whose Format mismatches readable.Format. Typical with platform-specific loaded bitmaps (e.g. D2D/Skia decoded textures) or an empty/placeholder Bitmap.","commonSituations":"Loading an image with a render backend that does not expose a readable framebuffer; using a bitmap produced by a GPU render target; calling CopyPixels on a Bitmap constructed but never given pixel data; cross-backend bitmaps where Format was set to a value the impl does not match.","solutions":["Use the transcoding overload CopyPixels(ILockedFramebuffer buffer) (or the writeable path) which handles format conversion and does not require the raw-Format match.","Confirm the bitmap was loaded/created from a source that yields an IReadableBitmapImpl before attempting raw pixel reads.","If you control creation, build the bitmap with a platform-supported format and through Load() / WriteableBitmap so the impl is readable.","Guard before calling: check Format != null and bitmap.PlatformImpl.Item is IReadableBitmapImpl."],"exampleFix":"// before\nbitmap.CopyPixels(rect, buffer, bufferSize, stride);\n\n// after - use the transcoding framebuffer overload\nvar size = rect.Size;\nusing var fb = new WriteableBitmap(PixelFormat.Rgba8888, AlphaFormat.Premultiplied, size, 96.0, 96.0);\nusing (var l = fb.Lock())\n    bitmap.CopyPixels(l);","handlingStrategy":"validation","validationCode":"// Validate the bitmap is readable in its own format before the raw CopyPixels call.\nbool CanCopyRaw(Bitmap b) =>\n    b.Format is not null\n    && b.PlatformImpl?.Item is IReadableBitmapImpl readable\n    && b.Format == readable.Format;\n\nif (!CanCopyRaw(bitmap))\n    throw new InvalidOperationException(\"Bitmap is not raw-readable; use the framebuffer overload.\");","typeGuard":"static bool IsRawReadable(Bitmap b) =>\n    b.Format is not null && b.PlatformImpl?.Item is IReadableBitmapImpl r && b.Format == r.Format;","tryCatchPattern":null,"preventionTips":["Prefer CopyPixels(ILockedFramebuffer) for arbitrary bitmaps — it handles transcoding.","Load/create bitmaps from sources that yield IReadableBitmapImpl when raw reads are needed.","Check Format != null before any raw pixel operation."],"tags":["imaging","bitmaps","copypixels","notsupported"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}