I’m working on an app that needs to write a .png image file from a Base64 encoded string. In an attempt to do this, I have the following code:
byte[] tempBytes = Convert.FromBase64String(base64EncodedString);
using (MemoryStream memoryStream = new MemoryStream(tempBytes))
{
System.Drawing.Image image = System.Drawing.Image.FromStream(memoryStream);
image.Save("C:\\inetpub\\wwwroot\\MySite\\test.png");
}
When this code executes, I receive the following error:
System.ArgumentException Parameter is not valid.
The relevant part of the stack trace looks like:
at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)
From my understanding, this means that my base64encodedString is not a valid image. However, I have no clue how to identify what the cause could be. The code above is run on the server. I’m passing the encoded string from a client app that i have also written. I’ve printed the encoded string on the client-side and also on the server side to ensure they match. Considering the two match, it would imply that I’m not encoding the string properly. How can I trace back the cause.
Thank you
You probably need to serialize the image first. (then deserialize on decode)