gooseman
gooseman

Reputation: 475

Error: Parameter is not valid. Converting byte array to image with Image.FromStream

    private bool IsValidImage(byte[] bytes)
    {
        try
        {
            using (MemoryStream ms = new MemoryStream(bytes))
                Image.FromStream(ms, true, true);
        }
        catch (ArgumentException)
        {
            return false;
        }
        return true;
    }

Can someone tell me what I'm doing wrong? This works fine when the actual byte array is an image. But when I pass in another type of file, like docx or xlsx then I get runtime error of "Parameter is not valid" on FromStream. The exception doesn't get caught in the catch block either. If the FromStream throws an ArgumentException, it should get caught in the catch block, right?

Upvotes: 0

Views: 296

Answers (1)

Fischermaen
Fischermaen

Reputation: 12458

An image can only be created from an image stored in the memory stream. If you store e.g. a ".docx"-File there, the image cannot be created from that.

Upvotes: 1

Related Questions