Click here to Skip to main content
15,867,945 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
I have a base64 string which is provided below. I guess its type is Application/Octet-stream.
I am using code below to convert it to an image. But it gives me error "Paramter is not valid!".
C#
string base64string = "AAAAAQEAAAEcEgABHxIA ... xYAAF4WQABcFwAAQ==";

using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(base64string)))
{
    pictureEdit1.Image = Image.FromStream(ms, true);
}


You can get full string from here: http://orion.mn/updateapp/base64_unicode_string.rar[^]!

Please, help me how to convert it to an image?
Posted
Updated 7-Jun-21 1:03am
v3
Comments
Richard MacCutchan 28-Mar-15 4:27am    
Paramter is not valid!
Where?
orgilhp 28-Mar-15 6:39am    
at this line: pictureEdit1.Image = Image.FromStream(ms, true);
Richard MacCutchan 28-Mar-15 7:56am    
Use your debugger to check whether the conversion worked. It may be that the MemoryStream object is not valid.
Kornfeld Eliyahu Peter 29-Mar-15 5:24am    
If it is an application/octet, what makes you believe, you can convert it into an image?
orgilhp 29-Mar-15 9:54am    
I provided base64string for download. Can you check it whether it is possible to convert to image.

1 solution

Hi,

I think your base 64 string is incorrect. How did you convert it to base 64 ?
You can validate your Base 64 string like this:
C#
public bool IsBase64String(string s)
{
  s = s.Trim();
  return (s.Length % 4 == 0) && Regex.IsMatch(s, @"^[a-zA-Z0-9\+/]*={0,3}$", RegexOptions.None);

}

Then you can call it like this:
C#
 public void LoadImage()
{
  string string64 = "R0lGODlhAQABAIAAAAAAAAAAACH5BAAAAAAALAAAAAABAAEAAAICTAEAOw==";
  bool isStringValid = this.IsBase64String(string64);
  if (isStringValid)
  {
     byte[] bytes = Convert.FromBase64String(string64);
     System.Drawing.Image image;
     using (MemoryStream ms = new MemoryStream(bytes))
     {
        image = System.Drawing.Image.FromStream(ms);
        //image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
     }                
   }            
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900