stride3d/stride · error · InvalidOperationException

Image size is different than expected.

Error message

Image size is different than expected.

What it means

After deserializing the image header and constructing the Image, LoadFromMemory reads an Int32 trailer with the total serialized size and compares it to image.TotalSizeInBytes. A mismatch means the stream data is inconsistent with its own header (truncated, corrupt, or written by a mismatched writer version), so the library throws InvalidOperationException rather than returning an image backed by wrong-length data.

Solutions

  1. Re-import/re-export the asset with the matching Stride version and reload.
  2. Verify the stream is complete (check length vs. expected size, re-download or re-copy the file).
  3. Wrap the call in try/catch and fall back to a default asset so one corrupt file does not crash loading.

Example fix

// before
var image = ImageHelper.LoadFromMemory(stream);
// after
try { var image = ImageHelper.LoadFromMemory(stream); }
catch (InvalidOperationException) { image = LoadDefaultTexture(); }
Defensive patterns

Strategy: try-catch

Validate before calling

// Preflight: check the stream has at least header + size-trailer room
if (stream.Length < 12) throw new InvalidDataException("Stride image stream too short");

Try / catch

try { return ImageHelper.LoadFromMemory(stream, makeACopy); }
catch (InvalidOperationException ex) { log.Warn($"Corrupt/truncated Stride image: {ex.Message}"); return LoadFallbackImage(); }

Prevention

When it happens

Trigger: Calling ImageHelper.LoadFromMemory on a Stride-format stream where the trailing Int32 size field differs from the computed image.TotalSizeInBytes — e.g. the stream was truncated, the header was tampered with, or the file was written by an incompatible Stride version.

Common situations: Corrupted or partially downloaded .sdtex asset files; assets produced by an older/newer Stride serialization version; hand-assembled byte streams in tests or tools; loading assets over an incomplete network transfer.

Understand the failure class

Background: Checksum mismatch errors: "checksum verification failed", "digest mismatch", "expected vs actual checksum" — what they mean and how to fix them — this error's family across 41 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/42683ed549b4ce06. Report an issue: GitHub.

Appendix: source

Thrown at sources/engine/Stride.Foundation/Graphics/ImageHelper.cs:50

                throw new NotSupportedException($"AlphaLoadMode.{alphaLoadMode} not supported for the Stride image format (no premultiplied-state metadata).");

            // Read header
            var imageDescription = new ImageDescription();
            ImageDescriptionSerializer.Serialize(ref imageDescription, ArchiveMode.Deserialize, stream);

            if (makeACopy)
            {
                var buffer = MemoryUtilities.Allocate(size);
                MemoryUtilities.CopyWithAlignmentFallback((void*)buffer, source: (void*)pSource, (uint)size);
                pSource = buffer;
                makeACopy = false;
            }

            var image = new Image(imageDescription, pSource, 0, handle, !makeACopy);

            var totalSizeInBytes = stream.ReadInt32();
            if (totalSizeInBytes != image.TotalSizeInBytes)
                throw new InvalidOperationException("Image size is different than expected.");

            // Read image data
            stream.Serialize(new Span<byte>((void*)image.DataPointer, image.TotalSizeInBytes));

            return image;
        }

        public static unsafe void SaveFromMemory(PixelBuffer[] pixelBuffers, int count, ImageDescription description, System.IO.Stream imageStream)
        {
            var stream = new BinarySerializationWriter(imageStream);

            // Write magic code
            stream.Write(MagicCode);

            // Write image header
            ImageDescriptionSerializer.Serialize(ref description, ArchiveMode.Serialize, stream);

            // Write total size

View on GitHub (pinned to 96fad776d2)