Why am I getting the exception "Parameter not valid" in my code:
MemoryStream ms = new MemoryStream(byteArrayIn);
System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
The length of byteArrayIn
is 169014. I am getting this exception despite the fact that no value in it is greater than 255.
My guess is that
byteArrayIn
doesn't contain valid image data.Please give more information though:
byteArrayIn
from, and are you sure it should contain a valid image?The "parameter is not valid" exception thrown by
Image.FromStream()
tells you that the stream is not a 'valid' or 'recognised' format. Watch the memory streams, especially if you are taking various offsets of bytes from a file.Example 2 will work, note that useEmbeddedColorManagement must be false for validateImageData to be valid.
May be easiest to debug by dumping the memory stream to a file and inspecting the content.
Which line is throwing the exception? The
new MemoryStream(...)
? or theImage.FromStream(...)
? And what is thebyteArrayIn
? Is it abyte[]
? I only ask because of the comment "And none of value in it is not greater than 255" - which of course is automatic for abyte[]
.As a more obvious question: does the binary actually contain an image in a sensible format?
For example, the following (although not great code) works fine: