views:

5776

answers:

14

Hi,

I have an urgent problem, i'm trying to convert a byte array to a bitmap but it always shows me: System.ArgumentException: Parameter is not valid.

my code is as follow:

Im passing the bytes through a webservice with:

 string DecodedString = string.Empty;
 DecodedString = System.Text.Encoding.GetEncoding(1251).GetString(bytes);
 sResult = sResult + "<Photo>" +XmlConvert.EncodeName(DecodedString) + "</Photo>";

and in my webPage:

byte[] bytes = (Byte[])System.Text.Encoding.GetEncoding(1251).GetBytes(XmlConvert.DecodeName(xDocument.SelectSingleNode("Response/Images/Photo").InnerText));
    System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes);

    System.Drawing.Bitmap b = new System.Drawing.Bitmap(ms);//(System.Drawing.Image.FromStream(ms));

Thanks in Advance..

+2  A: 

Try passing the string as a Base64:

string DecodedString = string.Empty;
DecodedString = System.Convert.ToBase64String(bytes)
sResult = sResult + "<Photo>" +XmlConvert.EncodeName(DecodedString) + "</Photo>";

...

byte[] bytes = System.Convert.FromBase64String(xDocument.SelectSingleNode("Response/Images/Photo").InnerText);
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes);
System.Drawing.Bitmap b = System.Drawing.Image.FromStream(ms);

You also won't need to use XmlConvert to encode/decode the string.

configurator
But if I dont use the enconde/decode the xml cannot be read it throws an invalid xml exception..
*Don't* put a using statement round the stream in this case. When you've called Image.FromStream, you *mustn't* close the stream, or the image will be broken. When you dispose the image, *that* will close the stream.
Jon Skeet
D'oh. I was in a hurry when I wrote the answer and didn't actually read the entire code :/
configurator
A: 

I had a similar problem recently, but using Silverlight. I ended up needing to create a Custom HTTP Handler in order to pass the byte[] that defined the image back as a stream.

See http://www.dotnetcurry.com/ShowArticle.aspx?ID=220

Edit: This allows you to avoid worrying about XML encoding, and passes the image back in Binary form... YMMV

vt100
Same error when trying this solution.
Where are you getting the byte[]? Are you sure the data within the array is correct?
vt100
the byte is coming from: System.IO.FileStream fs = new System.IO.FileStream(sPath, System.IO.FileMode.Open, System.IO.FileAccess.Read); int streamLength = Convert.ToInt32(fs.Length); bytes = new byte[streamLength]; fs.Read(bytes, 0, streamLength);in the web Service.
A: 

Now it shows:

An exception of type 'System.FormatException' occurred in App_Web_qfrgklax.dll but was not handled in user code

Additional information: Invalid character in a Base-64 string.

When:

byte[] bytes = System.Convert.FromBase64String(xDocument.SelectSingleNode("Response/Images/Photo").InnerText);
A: 

ok, my code in the webservice is now [without the encode]:

string DecodedString = string.Empty;
    DecodedString = System.Convert.ToBase64String(bytes);
    sResult = sResult + "<Photo>" + DecodedString + "</Photo>";

and my page [without the decode]

byte[] bytes = System.Convert.FromBase64String(xDocument.SelectSingleNode("Response/Images/Photo").InnerText);

    System.Drawing.Bitmap b;

    using (System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes))
    {
       b = new System.Drawing.Bitmap(System.Drawing.Image.FromStream(ms));
    }

But its still showing Parameter is not valid.

in the next line:

b = new System.Drawing.Bitmap(System.Drawing.Image.FromStream(ms));
A: 

And i've noticed tha this Parameter is not valid exception is always happening in the same line of code:

System.Drawing.Image.FromStream(ms));

And i can clearly see that the from stream method is receiving the parameter its expecting.

But i can't find the right way to do it yet.

A: 

Try this:

byte[] bytes = System.Convert.FromBase64String(xDocument.SelectSingleNode("Response/Images/Photo").InnerText);
System.Drawing.ImageConverter imageConverter = new System.Drawing.ImageConverter();
Image image = imageConverter.ConvertFrom(bytes) as Image;
System.Drawing.Bitmap b = new System.Drawing.BitMap(image);

EDIT

Take a look at this:

Transfer any files on Web services by c#

Phaedrus
Now the error is in this line and its the same **Parameter is Not Valid Error**System.Drawing.Image image = imageConverter.ConvertFrom(bytes) as System.Drawing.Image;
Check bytes.Length, make sure that you actually have bytes in the byte array.
Phaedrus
checked,still the same issue.the byte is coming from: System.IO.FileStream fs = new System.IO.FileStream(sPath, System.IO.FileMode.Open, System.IO.FileAccess.Read); int streamLength = Convert.ToInt32(fs.Length); bytes = new byte[streamLength]; fs.Read(bytes, 0, streamLength); in the web Service
Something is wrong with your byte array, I don't thing it's a valid image.
Phaedrus
but when no using a webservice the image shows correctly.
A: 

According to MSDN:

ArgumentException - The stream does not have a valid image format

I believe your problem is in the original byte[] array you are passing to the web service. According to one of your comments, you did:

System.IO.FileStream fs = new System.IO.FileStream(sPath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
int streamLength = Convert.ToInt32(fs.Length);
bytes = new byte[streamLength];
fs.Read(bytes, 0, streamLength);

fs.Read returns the number of bytes that have been read into the byte array; it doesn't always read the entire file!
Try using the StreamFile method from http://www.dotnetspider.com/resources/4515-convert-file-into-byte-array.aspx. (First result of Google search)

configurator
Ok, i'll try this and see if works, but when i dont use a web service the code wirks and the image shows.
No it does not work same error Parameter is no Valid
A: 

Ok, here is the entire webservice code:

    byte[] bytes;
    System.IO.FileStream fs = new System.IO.FileStream(sPath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
    int streamLength = Convert.ToInt32(fs.Length);
    bytes = new byte[streamLength];
    fs.Read(bytes, 0, streamLength);

    .......
    Here i form the response xml. 
    .......


 string DecodedString = string.Empty;
    DecodedString = System.Convert.ToBase64String(bytes);
    sResult = sResult + "<Photo>" + DecodedString + "</Photo>";

And decode string is the picture i want to display.

The thing is that without using a web service it works i mean without the webservice the picture shows.

+1  A: 

FINALLLYYYY,

i did it, with the help of all of you, here is my page code

 byte[] bytes = System.Convert.FromBase64String(xDocument.SelectSingleNode("Response/Images/Photo").InnerText);


   System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes);

   System.Drawing.Bitmap b = new System.Drawing.Bitmap(ms); //(Bitmap)System.Drawing.Image.FromStream(ms);

   System.Drawing.Imaging.FrameDimension frameDim;
   frameDim = new System.Drawing.Imaging.FrameDimension(b.FrameDimensionsList[0]);


   int NumberOfFrames = b.GetFrameCount(frameDim);
   string[] paths = new string[NumberOfFrames];

   for (int i = 0; i < NumberOfFrames; i++)
   {
     b.SelectActiveFrame(frameDim, i);

     System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(b);
     paths[i] = imagePathfile.Remove(imagePathfile.Length - 4, 4) + i.ToString() + ".gif";

     bmp.Save(paths[i], System.Drawing.Imaging.ImageFormat.Gif);
     //bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
     bmp.Dispose();
   }

    Image1.Src = paths[0];
    //Check if there's more than 1 image cause its a TIFF
    if (paths.Length>1)
    {
      Image2.Src = paths[1];  
    }

Thanks to all of you..... Luis.

A: 

Thanks for this..its working for me..but the problem am facing is that the image displaying does not have same original clarity..Can any one tell me what is the problem? What should i do to maintain the same original clarity after decode..

Tnx in advance. Sri

A: 

actually i had been meet this problem, In my case, when i use IE browser, it is ok but when use another browser it always have the same error.

"Parameter is not valid exception is always happening in the same line of code: System.Drawing.Image.FromStream(ms));"

So i think it seems this issue depend on browser and type of image (JPEG,JPEG2000).

A: 

you can do this by simply creating a memory stream MemoryStream ms = new MemoryStream(bytes); Image img = System.Drawing.Image.FromStream(ms); for more detail visit this post

bimbim.in