{"record":{"id":"42683ed549b4ce06","repo":"stride3d/stride","slug":"image-size-is-different-than-expected","errorCode":null,"errorMessage":"Image size is different than expected.","messagePattern":"Image size is different than expected\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Foundation/Graphics/ImageHelper.cs","lineNumber":50,"sourceCode":"                throw new NotSupportedException($\"AlphaLoadMode.{alphaLoadMode} not supported for the Stride image format (no premultiplied-state metadata).\");\n\n            // Read header\n            var imageDescription = new ImageDescription();\n            ImageDescriptionSerializer.Serialize(ref imageDescription, ArchiveMode.Deserialize, stream);\n\n            if (makeACopy)\n            {\n                var buffer = MemoryUtilities.Allocate(size);\n                MemoryUtilities.CopyWithAlignmentFallback((void*)buffer, source: (void*)pSource, (uint)size);\n                pSource = buffer;\n                makeACopy = false;\n            }\n\n            var image = new Image(imageDescription, pSource, 0, handle, !makeACopy);\n\n            var totalSizeInBytes = stream.ReadInt32();\n            if (totalSizeInBytes != image.TotalSizeInBytes)\n                throw new InvalidOperationException(\"Image size is different than expected.\");\n\n            // Read image data\n            stream.Serialize(new Span<byte>((void*)image.DataPointer, image.TotalSizeInBytes));\n\n            return image;\n        }\n\n        public static unsafe void SaveFromMemory(PixelBuffer[] pixelBuffers, int count, ImageDescription description, System.IO.Stream imageStream)\n        {\n            var stream = new BinarySerializationWriter(imageStream);\n\n            // Write magic code\n            stream.Write(MagicCode);\n\n            // Write image header\n            ImageDescriptionSerializer.Serialize(ref description, ArchiveMode.Serialize, stream);\n\n            // Write total size","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Foundation/Graphics/ImageHelper.cs#L32-L68","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Re-import/re-export the asset with the matching Stride version and reload.","Verify the stream is complete (check length vs. expected size, re-download or re-copy the file).","Wrap the call in try/catch and fall back to a default asset so one corrupt file does not crash loading."],"exampleFix":"// before\nvar image = ImageHelper.LoadFromMemory(stream);\n// after\ntry { var image = ImageHelper.LoadFromMemory(stream); }\ncatch (InvalidOperationException) { image = LoadDefaultTexture(); }","handlingStrategy":"try-catch","validationCode":"// Preflight: check the stream has at least header + size-trailer room\nif (stream.Length < 12) throw new InvalidDataException(\"Stride image stream too short\");","typeGuard":null,"tryCatchPattern":"try { return ImageHelper.LoadFromMemory(stream, makeACopy); }\ncatch (InvalidOperationException ex) { log.Warn($\"Corrupt/truncated Stride image: {ex.Message}\"); return LoadFallbackImage(); }","preventionTips":["Validate asset integrity (hash/size) at build or download time.","Pin the Stride version used to export assets to the runtime version.","Never hand-edit serialized image streams."],"tags":["graphics","image-loading","data-corruption","size-mismatch"],"backgroundTag":"checksum-mismatch","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}