0
    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?

1
  • try debugging your code and check whether you are getting values or not Commented Apr 7, 2015 at 13:46

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

So shouldn't the exception be caught in my catch block? How do I achieve what I'm trying to do? I don't know ahead of time if the byte array is going to be an image or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.