System.ArgumentException: Parameter is not valid
See the question and my original answer on StackOverflowThe posted code seems correct. I have tested it and it works fine.
The exception "System.ArgumentException: Parameter is not valid." without any other hint (especially not the name of the parameter) is a wrapper for GDI+ (the underlying technology behind .NET Image class) standard InvalidParameter error, which does not tell use exactly what parameter is invalid.
So, following the FromStream
code with .NET Reflector, we can see that the parameters used in GDI+ calls are essentially ... the input stream.
So my guess is the input stream you provide is sometimes invalid as an image? You should save the failing input streams (using File.SaveAllBytes(sf) for example) for further investigation.