{"record":{"id":"b3cab9396ef1c387","repo":"SixLabors/ImageSharp","slug":"trying-to-copy-swap-incompatible-buffers-this-is-most-likely","errorCode":null,"errorMessage":"Trying to copy/swap incompatible buffers. This is most likely caused by applying an unsupported processor to wrapped-memory images.","messagePattern":"Trying to copy/swap incompatible buffers\\. This is most likely caused by applying an unsupported processor to wrapped-memory images\\.","errorType":"exception","errorClass":"InvalidMemoryOperationException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Memory/Buffer2D{T}.cs","lineNumber":408,"sourceCode":"    {\n        bool swapped = false;\n        if (MemoryGroup<T>.CanSwapContent(destination.FastMemoryGroup, source.FastMemoryGroup))\n        {\n            (destination.FastMemoryGroup, source.FastMemoryGroup) = (source.FastMemoryGroup, destination.FastMemoryGroup);\n            destination.FastMemoryGroup.RecreateViewAfterSwap();\n            source.FastMemoryGroup.RecreateViewAfterSwap();\n            swapped = true;\n        }\n        else\n        {\n            long sourceLayoutLength = GetRequiredLength(source.Width, source.Height, source.RowStride);\n            long destinationLayoutLength = GetRequiredLength(destination.Width, destination.Height, destination.RowStride);\n\n            bool destinationCanRepresentSource = destination.FastMemoryGroup.TotalLength >= sourceLayoutLength;\n            bool sourceCanRepresentDestination = source.FastMemoryGroup.TotalLength >= destinationLayoutLength;\n            if (!destinationCanRepresentSource || !sourceCanRepresentDestination)\n            {\n                throw new InvalidMemoryOperationException(\n                    \"Trying to copy/swap incompatible buffers. This is most likely caused by applying an unsupported processor to wrapped-memory images.\");\n            }\n\n            source.CopyTo(destination);\n        }\n\n        (destination.Size, source.Size) = (source.Size, destination.Size);\n        (destination.RowStride, source.RowStride) = (source.RowStride, destination.RowStride);\n        return swapped;\n    }\n\n    [MethodImpl(InliningOptions.ColdPath)]\n    private void ThrowYOutOfRangeException(int y)\n        => throw new ArgumentOutOfRangeException($\"DangerousGetRowSpan({y}). Y was out of range. Height={this.Height}\");\n\n    [MethodImpl(InliningOptions.ShortMethod)]\n    private static long GetRequiredLength(int width, int height, int stride)\n        => checked(((long)(height - 1) * stride) + width);","sourceCodeStart":390,"sourceCodeEnd":426,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Memory/Buffer2D{T}.cs#L390-L426","documentation":"Buffer2D<T>.SwapOrCopyContent can only swap or copy when each buffer's memory group is large enough to represent the other's layout. When an image wraps externally provided memory that is too small (common with unsupported processors on wrapped-memory images), InvalidMemoryOperationException is thrown.","triggerScenarios":"Calling SwapOrCopyContent between buffers whose FastMemoryGroup.TotalLength cannot represent the other side's required layout (width/height/row stride), typically after applying a processor that reallocates to a wrapped-memory image.","commonSituations":"Applying resize/transform processors to images created over user-supplied Memory<T> via Image.WrapMemory; the processor's output buffer does not fit the wrapped allocation.","solutions":["Clone the wrapped-memory image into a normally allocated image before applying processors","Increase the size of the wrapped memory to fit the required width*height (and stride) layout","Use processors that operate in-place and do not require buffer reallocation"],"exampleFix":"// before\nusing var img = Image.WrapMemory<Rgba32>(memory, w, h);\nimg.Mutate(x => x.Resize(w * 2, h * 2)); // reallocates: incompatible\n// after\nusing var img = Image.LoadPixelData<Rgba32>(span, w, h);\nimg.Mutate(x => x.Resize(w * 2, h * 2));","handlingStrategy":"validation","validationCode":"if ((long)buf.Width * buf.Height > memory.Length) throw new InvalidOperationException(\"Wrapped memory too small for target layout.\");","typeGuard":"bool fitsWrapped<T>(Memory<T> mem, int w, int h, int stride) => mem.Length >= (long)(h - 1) * stride + w;","tryCatchPattern":"try { buffer.SwapOrCopyContent(other); } catch (InvalidMemoryOperationException) { /* clone image and retry */ }","preventionTips":["Avoid mutating wrapped-memory images with processors that reallocate","Size wrapped memory to the maximum layout the pipeline can produce","Clone to normally-allocated images before running transform pipelines"],"tags":["csharp","memory","buffers"],"backgroundTag":"incompatible-source-type","analyzedSha":"59ce6af6fc29027cda277ef62d4d1694a8acce91","analyzedAt":"2026-09-13T18:34:59.331Z","contentChangedAt":"2026-09-13T18:34:59.331Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}