0

I have a base64 string that looks like this:

data:image/png;base64,iVBORw0KGgoA....

And I need to convert the string to an image as byte array so I do:

var b = Convert.FromBase64String(base64string);

But I get an exception:

System.FormatException: 'The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

What am I doing wrong?

Ivan Debono
  • 457
  • 3
  • 14
  • 1
    Does this answer your question? [The input is not a valid Base-64 string as it contains a non-base 64 character](https://stackoverflow.com/questions/15114044/the-input-is-not-a-valid-base-64-string-as-it-contains-a-non-base-64-character) – shingo Jul 12 '23 at 04:32

1 Answers1

3

You didn't remove the "data:image/png;base64," part of the string which contains redundant text helpful for browsers to recognize it as image. You can remove it using following code:

var text = "insert your text here";
var header = "base64,";
var startpos = text.IndexOf(header) + header.Length;
var decode = Convert.FromBase64String(text[startpos..]);