{"record":{"id":"b020d599012e5e28","repo":"AvaloniaUI/Avalonia","slug":"copypixels-is-not-supported-for-transcoded-bitmaps","errorCode":null,"errorMessage":"CopyPixels is not supported for transcoded bitmaps","messagePattern":"CopyPixels is not supported for transcoded bitmaps","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Media/Imaging/Bitmap.cs","lineNumber":289,"sourceCode":"            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            {\n                // Since we can't read pixels from the bitmap, we need to render it to a compatible bitmap and read pixels from it.\n                using var rtb = new RenderTargetBitmap(PixelSize);\n                using (var ctx = rtb.CreateDrawingContext())\n                    ctx.DrawImage(this, new Rect(rtb.Size));","sourceCodeStart":271,"sourceCodeEnd":307,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Media/Imaging/Bitmap.cs#L271-L307","documentation":"Thrown by the raw CopyPixels(IntPtr...) overload when _isTranscoded is true. A transcoded bitmap stores pixels in Rgba8888 internally (via BitmapMemory) because the requested format was not natively supported by the platform; the raw read path is disabled for such bitmaps to avoid returning pixels in an unexpected format. Use the transcoding-aware CopyPixels(ILockedFramebuffer) overload which reconciles formats.","triggerScenarios":"Creating a WriteableBitmap (or loading a bitmap) with a pixel format the platform backend does not support natively (WriteableBitmap.CreatePlatformImpl falls back to a BitmapMemory Rgba8888 buffer and marks the bitmap transcoded), then calling the IntPtr CopyPixels overload.","commonSituations":"Requesting an exotic format (e.g. Bgr555, Gray16) on a platform whose IPlatformRenderInterface does not list it as supported; reading pixels back from a transcoded WriteableBitmap used for offscreen rendering.","solutions":["Switch to CopyPixels(ILockedFramebuffer buffer), which transcodes to the locked buffer's format.","Create the bitmap in a natively supported format (commonly Rgba8888/Bgra8888) so the transcoded fallback is not triggered.","Before reading, check whether the bitmap is transcoded and route to the framebuffer overload accordingly."],"exampleFix":"// before - throws on a transcoded bitmap\nbitmap.CopyPixels(srcRect, ptr, bufSize, stride);\n\n// after\nusing var fb = lockedTarget; // ILockedFramebuffer with desired PixelFormat\nbitmap.CopyPixels(fb);","handlingStrategy":"validation","validationCode":"// Route transcoded bitmaps to the framebuffer (transcoding) overload.\nvoid CopySafe(Bitmap b, ILockedFramebuffer target)\n{\n    // The IntPtr overload throws for transcoded bitmaps; use the framebuffer overload instead.\n    b.CopyPixels(target);\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    bitmap.CopyPixels(srcRect, ptr, bufSize, stride);\n}\ncatch (NotSupportedException)\n{\n    // Fall back to the transcoding-aware framebuffer overload.\n    bitmap.CopyPixels(lockedTarget);\n}","preventionTips":["Create bitmaps in a natively supported format (Rgba8888/Bgra8888) to avoid the transcoded flag.","Use the ILockedFramebuffer CopyPixels overload when reading arbitrary bitmaps.","Do not assume the IntPtr overload works for all bitmap types."],"tags":["imaging","bitmaps","copypixels","transcode","notsupported"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}