{"record":{"id":"75948da5ad6b6d70","repo":"dotnet/machinelearning","slug":"invalid-input-stream-contents","errorCode":null,"errorMessage":"Invalid input stream contents","messagePattern":"Invalid input stream contents","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.ImageAnalytics/MLImage.cs","lineNumber":46,"sourceCode":"        {\n        }\n\n        /// <summary>\n        /// Create a new MLImage instance from a stream.\n        /// </summary>\n        /// <param name=\"imageStream\">The stream to create the image from.</param>\n        /// <returns>MLImage object.</returns>\n        public static MLImage CreateFromStream(Stream imageStream)\n        {\n            if (imageStream is null)\n            {\n                throw new ArgumentNullException(nameof(imageStream));\n            }\n\n            SKBitmap image = SKBitmap.Decode(imageStream);\n            if (image is null)\n            {\n                throw new ArgumentException($\"Invalid input stream contents\", nameof(imageStream));\n            }\n\n            return new MLImage(image);\n        }\n\n        /// <summary>\n        /// Create a new MLImage instance from a stream.\n        /// </summary>\n        /// <param name=\"imagePath\">The image file path to create the image from.</param>\n        /// <returns>MLImage object.</returns>\n        public static MLImage CreateFromFile(string imagePath)\n        {\n            if (imagePath is null)\n            {\n                throw new ArgumentNullException(nameof(imagePath));\n            }\n\n            SKBitmap image = SKBitmap.Decode(imagePath);","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.ImageAnalytics/MLImage.cs#L28-L64","documentation":"MLImage.CreateFromStream decodes the stream with SkiaSharp (SKBitmap.Decode). If Skia cannot decode any bitmap from the bytes (null result) — meaning the content is not a supported image or the data is truncated/corrupt — it throws an ArgumentException naming imageStream.","triggerScenarios":"Calling MLImage.CreateFromStream with a stream whose contents are not a decodable image (HTML error page saved as .jpg, zero-byte file, truncated download, unsupported codec like WebP/HEIC on older Skia, or stream positioned past the data).","commonSituations":"Downloading images over HTTP without checking the response is actually an image; reading partially-written files while another process is still writing them; streams opened without rewinding (Position at end).","solutions":["Verify the stream contains valid image bytes (magic-number check: JPEG FF D8, PNG 89 50 4E 47, etc.) before calling","Rewind the stream (stream.Position = 0) before decoding","Re-download / re-copy the file — the bytes are likely truncated or an error page","Convert unsupported formats (HEIC, exotic WebP) to PNG/JPEG first"],"exampleFix":"// before\nusing var fs = File.OpenRead(path);\nvar img = MLImage.CreateFromStream(fs); // Position at end -> decode fails\n// after\nusing var fs = File.OpenRead(path);\nfs.Position = 0;\nbyte[] head = new byte[4];\nif (fs.Read(head, 0, 4) < 4 || !IsKnownImageMagic(head))\n    throw new InvalidDataException($\"{path} is not a recognizable image.\");\nfs.Position = 0;\nvar img = MLImage.CreateFromStream(fs);","handlingStrategy":"validation","validationCode":"static bool LooksLikeImage(Stream s)\n{\n    long pos = s.Position; s.Position = 0;\n    Span<byte> b = stackalloc byte[4];\n    int n = s.Read(b);\n    s.Position = pos;\n    return n >= 3 && (b[0] == 0xFF && b[1] == 0xD8 // JPEG\n        || b[0] == 0x89 && b[1] == 0x50 // PNG\n        || b[0] == 0x42 && b[1] == 0x4D // BMP\n        || b[0] == 0x47 && b[1] == 0x49); // GIF\n}","typeGuard":"bool IsDecodableImageStream(Stream s) => s != null && s.CanRead && s.Length > 0 && LooksLikeImage(s);","tryCatchPattern":"try\n{\n    img = MLImage.CreateFromStream(stream);\n}\ncatch (ArgumentException ex) when (ex.ParamName == \"imageStream\")\n{\n    logger.LogWarning(\"Undecodable image stream: {Reason}\", ex.Message);\n}","preventionTips":["Rewind streams (Position = 0) before decoding","Check HTTP responses are images (content-type + magic bytes) before passing the body","Verify downloads complete (size/content-length match) before use"],"tags":["csharp","images","skia","decoding"],"backgroundTag":"invalid-argument-format","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}