views:

6214

answers:

5

Hi i am getting the same exception "Parameter not valid" in my code

MemoryStream ms = new MemoryStream(byteArrayIn);
System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);

Actually the byteArrayIn 's length is 169014. And none of value in it is not greater than 255. But i am getting this exception.

So my question is why i am getting this exception? How to solve?

Please help me!!

Thanks!!

+1  A: 

Which line is throwing the exception? The new MemoryStream(...)? or the Image.FromStream(...)? And what is the byteArrayIn? Is it a byte[]? I only ask because of the comment "And none of value in it is not greater than 255" - which of course is automatic for a byte[].

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:

    byte[] data = File.ReadAllBytes(@"d:\extn.png"); // not a good idea...
    MemoryStream ms = new MemoryStream(data);
    Image img = Image.FromStream(ms);
    Console.WriteLine(img.Width);
    Console.WriteLine(img.Height);
Marc Gravell
+3  A: 

My guess is that byteArrayIn doesn't contain valid image data.

Please give more information though:

  • Which line of code is throwing an exception?
  • What's the message?
  • Where did you get byteArrayIn from, and are you sure it should contain a valid image?
Jon Skeet
Thanks Jon, this answer allowed me to find a solution to my Parameter is not valid exception. +1 for you
Sebastian
+1  A: 

Hi, Thanks for your response

1) Exception has been in this following line.. System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);

2)byteArrayIn is a byte array...

3)I am capturing image from the motionsensor... After long processing i got an array byteArrayIn...

When i pass this array to System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms); it gives that Exception..

what to do??

Thank you!!

A: 

I have the exact same problem and I have not yet been able to manage it. Any ideas at all? Thanks

Izabela
this is not an answer.
Sebastian Good
+1  A: 

I had the same problem and apparently is solved now, despite this and some other gdi+ exceptions are very misleading, I found that actually the problem was that the parameter being sent to a Bitmap constructor was not valid. I have this code:

using (System.IO.FileStream fs = new System.IO.FileStream(inputImage, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite))
            {
                try
                {
                    using (Bitmap bitmap = (Bitmap)Image.FromStream(fs, true, false))
                    {
                        try
                        {
                            bitmap.Save(OutputImage + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);

                            GC.Collect();
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }
                }
                catch (ArgumentException aex)
                {
                    throw new Exception("The file received from the Map Server is not a valid jpeg image", aex);
                }
            }

The following line was causing an error:

Bitmap bitmap = (Bitmap)Image.FromStream(fs, true, false)

The file stream was built from the file downloaded from the Map Server. My app was sending the request incorrectly to get the image, and the server was returning something with the jpg extension, but was actually a html telling me that an error ocurred. So I was taking that image and trying to build a Bitmap with it. The fix was to control/ validate the image for a valid jpeg image. More info on a post I wrote about it:Parameter is not valid GDI+ error

Hope it helps!

Sebastian