AvaloniaUI/Avalonia · error · NotSupportedException

CopyPixels is not supported for transcoded bitmaps

Error message

CopyPixels is not supported for transcoded bitmaps

What it means

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.

Source

Thrown at src/Avalonia.Base/Media/Imaging/Bitmap.cs:289

            ILockedFramebuffer fb)
        {
            sourceRect = ValidateSourceRect(sourceRect);
            CopyPixelsCore(sourceRect, fb.Address, fb.RowBytes, fb.Format, buffer, bufferSize, stride);
        }

        public virtual void CopyPixels(PixelRect sourceRect, IntPtr buffer, int bufferSize, int stride)
        {
            if (
                Format == null
                || PlatformImpl.Item is not IReadableBitmapImpl readable
                || Format != readable.Format
            )
            {
                throw new NotSupportedException("CopyPixels is not supported for this bitmap type");
            }

            if (_isTranscoded)
                throw new NotSupportedException("CopyPixels is not supported for transcoded bitmaps");

            using (var fb = readable.Lock())
                CopyPixelsCore(sourceRect, buffer, bufferSize, stride, fb);
        }

        /// <summary>
        /// Copies pixels to the target buffer and transcodes the pixel and alpha format if needed.
        /// </summary>
        /// <param name="buffer">The target buffer.</param>
        /// <exception cref="NotSupportedException"></exception>
        public void CopyPixels(ILockedFramebuffer buffer)
        {
            if (PlatformImpl.Item is not IReadableBitmapImpl readable || readable.Format == null || readable.AlphaFormat == null)
            {
                // Since we can't read pixels from the bitmap, we need to render it to a compatible bitmap and read pixels from it.
                using var rtb = new RenderTargetBitmap(PixelSize);
                using (var ctx = rtb.CreateDrawingContext())
                    ctx.DrawImage(this, new Rect(rtb.Size));

View on GitHub (pinned to 11c5427268)

Solutions

  1. Switch to CopyPixels(ILockedFramebuffer buffer), which transcodes to the locked buffer's format.
  2. Create the bitmap in a natively supported format (commonly Rgba8888/Bgra8888) so the transcoded fallback is not triggered.
  3. Before reading, check whether the bitmap is transcoded and route to the framebuffer overload accordingly.

Example fix

// before - throws on a transcoded bitmap
bitmap.CopyPixels(srcRect, ptr, bufSize, stride);

// after
using var fb = lockedTarget; // ILockedFramebuffer with desired PixelFormat
bitmap.CopyPixels(fb);
Defensive patterns

Strategy: validation

Validate before calling

// Route transcoded bitmaps to the framebuffer (transcoding) overload.
void CopySafe(Bitmap b, ILockedFramebuffer target)
{
    // The IntPtr overload throws for transcoded bitmaps; use the framebuffer overload instead.
    b.CopyPixels(target);
}

Try / catch

try
{
    bitmap.CopyPixels(srcRect, ptr, bufSize, stride);
}
catch (NotSupportedException)
{
    // Fall back to the transcoding-aware framebuffer overload.
    bitmap.CopyPixels(lockedTarget);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/b020d599012e5e28. Report an issue: GitHub.