Reputation: 35
I am try to upload image from an iPhone using a webservice but I got an Exception like -
{System.ArgumentException: Parameter is not valid. at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)}
in following code
string acFolder = Server.MapPath("~/Images/");
string imgname = DateTime.UtcNow.ToString().Replace(" ", "").Replace("AM", "").Replace("PM", "").Replace("/", "").Replace("-", "").Replace(":", "") + ".jpeg";
byte[] imageBytes = Convert.FromBase64String(image.Replace(" ", "+"));
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
System.Drawing.Image image2 = System.Drawing.Image.FromStream(ms,true,true);
image2.Save(acFolder + imgname);
I got the exception in this line
System.Drawing.Image image2 = System.Drawing.Image.FromStream(ms,true,true);
Upvotes: 1
Views: 16830
Reputation: 42363
Given that it's an ArgumentException
it's to do with one of the arguments passed to the FromStream
method.
If you open the second of the two links above you'll see from the documentation that an ArgumentException
is raised when the stream passed does not represent a valid image format (you can confirm this by checking the exception's ParamName property.
So that means the format of the image that is being uploaded is not supported by the Image
class. Either that, or the bytes of the image are being screwed in some way. That would seem to be supported by your own code - where you replace the '+' with ' ' in the base 64 string. Base64 is not intended to have spaces in it - take that line of code out.
Update
Since you say it doesn't work without it - I'm guessing the data is being passed in a manner where an incoming '+'
is being interpreted as a space and that's why you're trying to reinstate them. If sent in a request body this shouldn't happen, so since it is I'm guessing the iPhone app and your server need to use Modified base64 for URLs instead.
Upvotes: 1