dotnet/machinelearning · error · ArgumentException
Invalid input stream contents
Error message
Invalid input stream contents
What it means
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.
Source
Thrown at src/Microsoft.ML.ImageAnalytics/MLImage.cs:46
{
}
/// <summary>
/// Create a new MLImage instance from a stream.
/// </summary>
/// <param name="imageStream">The stream to create the image from.</param>
/// <returns>MLImage object.</returns>
public static MLImage CreateFromStream(Stream imageStream)
{
if (imageStream is null)
{
throw new ArgumentNullException(nameof(imageStream));
}
SKBitmap image = SKBitmap.Decode(imageStream);
if (image is null)
{
throw new ArgumentException($"Invalid input stream contents", nameof(imageStream));
}
return new MLImage(image);
}
/// <summary>
/// Create a new MLImage instance from a stream.
/// </summary>
/// <param name="imagePath">The image file path to create the image from.</param>
/// <returns>MLImage object.</returns>
public static MLImage CreateFromFile(string imagePath)
{
if (imagePath is null)
{
throw new ArgumentNullException(nameof(imagePath));
}
SKBitmap image = SKBitmap.Decode(imagePath);View on GitHub (pinned to 7b76e69cf9)
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
Example fix
// before
using var fs = File.OpenRead(path);
var img = MLImage.CreateFromStream(fs); // Position at end -> decode fails
// after
using var fs = File.OpenRead(path);
fs.Position = 0;
byte[] head = new byte[4];
if (fs.Read(head, 0, 4) < 4 || !IsKnownImageMagic(head))
throw new InvalidDataException($"{path} is not a recognizable image.");
fs.Position = 0;
var img = MLImage.CreateFromStream(fs); Defensive patterns
Strategy: validation
Validate before calling
static bool LooksLikeImage(Stream s)
{
long pos = s.Position; s.Position = 0;
Span<byte> b = stackalloc byte[4];
int n = s.Read(b);
s.Position = pos;
return n >= 3 && (b[0] == 0xFF && b[1] == 0xD8 // JPEG
|| b[0] == 0x89 && b[1] == 0x50 // PNG
|| b[0] == 0x42 && b[1] == 0x4D // BMP
|| b[0] == 0x47 && b[1] == 0x49); // GIF
} Type guard
bool IsDecodableImageStream(Stream s) => s != null && s.CanRead && s.Length > 0 && LooksLikeImage(s);
Try / catch
try
{
img = MLImage.CreateFromStream(stream);
}
catch (ArgumentException ex) when (ex.ParamName == "imageStream")
{
logger.LogWarning("Undecodable image stream: {Reason}", ex.Message);
} Prevention
- 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
When it happens
Trigger: 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).
Common situations: 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).
Understand the failure class
Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.
Related errors
- Invalid path
- Directory "{0}" does not exist.
- File {path} too big to open.
- Image pixel format is not supported
- Invalid image resizing mode value
AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11).
Data as JSON: /api/errors/75948da5ad6b6d70.
Report an issue: GitHub.