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?

1

1 Answer 1

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..]);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.