Getting error while converting base64 string to image

While trying to convert Base64 string in c#, getting following error:

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.

My Intent is to save diagram image on server, any other alternatives to achieve the functionality are also welcomed.

Thanks

What’s the data string?

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAlAAAALSCAYAAAASmu+WAAAgAElEQVR4XuxdB5Qc1bG9PZtz0OaglbSSEMoCFADLwoAx0cYfk74BkTE2SSIoEDQiSPDJwcYmGAv8v0GAwSLZ2GAwUSIoC+W4Oee80//U076lp3fyzvTM9NQ7h4N2t/uFWy/crqpXpYALI8AIMAKMACPACDACjIBXCChePc0PMwKMACPACDACjAAjwAiACRRPAkaAEWAEGAFGgBFgBLxEgAmUl4Dx44wAI8AIMAKMACPACDCB4jnACDACjAAjwAgwAoyAlwgwgfISMH6cEWAEGAFGgBFgBBgBJlA8BxgBRoARYAQYAUaAEfASASZQXgLGjzMCjAAjwAgwAowAI8AEiucAI8AIMAKMACPACDACXiLABMpLwPhxRoARYAQYAUaAEWAEmEDxHGAEGAFGgBEIKQTqe965IqQ6ZJbOKOrmETFnrjPLcII9DiZQwZYAt88IMAKMACNgh0Bz7zvP2lT1SoYlAAgo2A1VWaPY1LfS48/8KAAtREyVTKAiRtQ8UEaAEWAEwgMBJlDGyEmBUgFgjU1V38qMO/NdY1o1TytMoMwjSx4JI8AIMAKmQIAJVDDEqDQpCtb029S3MmM71ijKeT3B6EU4tckEKpykxX1lBEyKwK3rkBdjQSlsKFSBTKiIV1S02yyoUxSUdbZj12MnoMmkw+dh6RBgAhXsKaH0WJTDmiklpm9NunI2rz0HImECFex5yu0zAhGIwOKvMMai4gyo+JEKzAGQ7w4GhXw3gM8VFR/0J+Ct+6ei0d07/PfwRIAJVGjJTVGUd22wvWWLtq3JUn5GZj8uAJhA8TRgBBgBwxBYsg6/hIrLAJzkh0ZXA3h+5Wy874e6uIoQQoAJVAgJQ98VRflIsdneUlR1TVr8T+mjJmILE6iIFT0PnBEwDoElX+FiqFgMFRP93aqi4ENbP1befyz+5…aAASoxzhyFApYQqK3AzwDcYIlmAEhg1SwPnrZKP+yDAhRInAADVOKsORIFLCFQW4lNkPii2ZuRAg/PqsIPzN4H66cABZIjwACVHHeOSgHTCrx3DvJzvPiHBE43axNC4P9cVfiGWetn3RSgQPIFGKCSPwesgAKmE9ixGNPSHdgkgVPMVrwA/uTy2Ofp6mabH9ZLAbMIMECZZaZYJwUMJrD7HEz3+vBHSHzOYKWNVs5LZR583UT1slQKUMCgAgxQBp0YlkUBMwjsPx8nHe3BbwGcb4J6N5R58F0T1MkSKUABEwgwQJlgklgiBYwuUFuJ9ZC4xqh1CoFbXVV40Kj1sS4KUMB8AgxQ5pszVkwBQwrUV+DbfoHHIZFplAKFwE6/xA2zPPibUWpiHRSggDUEGKCsMY/sggKGEFD7onw+PCAlLk12QRJ4pKwYt4gX4Ut2LRyfAhSwngADlPXmlB1RIOkC9ZX4sl9iNYAlSSjmJThwX9nreCcJY3NIClDAJgIMUDaZaLZJgWQI1J6Nrwg/rpHAhfEeXwDPSon1ZdV4I95j8foUoAAFGKD4HqAABeIusHsp5np9uBQCX43xYw9ek8AfHH4859qKg3FvhANQgAIUOC7AAMW3AgUokFCBPRWY6RVYColFUuA0qIdx6tl4LtAiJLZLgXekxFa/E6+evAWHE1o8B6MABSjAAMX3AAUoYBSBTxeh1JmKUp/EBIdErgAypEC3BNqcDjSlCewtYVgyynSxDgpQAABXoPg2oAAFKEABClCAAhEKMEBFCMbDKUABClCAAhSgAAMU3wMUoAAFKEABClAgQgEGqAjBeDgFKEABClCAAhRggOJ7gAIUoAAFKEABCkQowAAVIRgPpwAFKEABClCAAgxQfA9QgAIUoAAFKECBCAUYoCIE4+EUoAAFKEABClCAAYrvAQpQgAIUoAAFKBChAANUhGA8nAIUoAAFKEABCvw/jH+P6V9fhVwAAAAASUVORK5CYII=

And what did you use in your C# code?

        var base64String = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUg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3RSgQPIFGKCSPwesgAKmE9ixGNPSHdgkgVPMVrwA/uTy2Ofp6mabH9ZLAbMIMECZZaZYJwUMJrD7HEz3+vBHSHzOYKWNVs5LZR583UT1slQKUMCgAgxQBp0YlkUBMwjsPx8nHe3BbwGcb4J6N5R58F0T1MkSKUABEwgwQJlgklgiBYwuUFuJ9ZC4xqh1CoFbXVV40Kj1sS4KUMB8AgxQ5pszVkwBQwrUV+DbfoHHIZFplAKFwE6/xA2zPPibUWpiHRSggDUEGKCsMY/sggKGEFD7onw+PCAlLk12QRJ4pKwYt4gX4Ut2LRyfAhSwngADlPXmlB1RIOkC9ZX4sl9iNYAlSSjmJThwX9nreCcJY3NIClDAJgIMUDaZaLZJgWQI1J6Nrwg/rpHAhfEeXwDPSon1ZdV4I95j8foUoAAFGKD4HqAABeIusHsp5np9uBQCX43xYw9ek8AfHH4859qKg3FvhANQgAIUOC7AAMW3AgUokFCBPRWY6RVYColFUuA0qIdx6tl4LtAiJLZLgXekxFa/E6+evAWHE1o8B6MABSjAAMX3AAUoYBSBTxeh1JmKUp/EBIdErgAypEC3BNqcDjSlCewtYVgyynSxDgpQAABXoPg2oAAFKEABClCAAhEKMEBFCMbDKUABClCAAhSgAAMU3wMUoAAFKEABClAgQgEGqAjBeDgFKEABClCAAhRggOJ7gAIUoAAFKEABCkQowAAVIRgPpwAFKEABClCAAgxQfA9QgAIUoAAFKECBCAUYoCIE4+EUoAAFKEABClCAAYrvAQpQgAIUoAAFKBChAANUhGA8nAIUoAAFKEABCvw/jH+P6V9fhVwAAAAASUVORK5CYII=";
        byte[] bytes = Convert.FromBase64String(base64String);
        var base64String = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAlAAAALSCAYAAAASmu+WAAAgAElEQVR4XuxdB5Qc1bG9PZtz0OaglbSSEMoCFADLwoAx0cYfk74BkTE2SSIoEDQiSPDJwcYmGAv8v0GAwSLZ2GAwUSIoC+W4Oee80//U076lp3fyzvTM9NQ7h4N2t/uFWy/crqpX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EwgwQJlgklgiBYwuUFuJ9ZC4xqh1CoFbXVV40Kj1sS4KUMB8AgxQ5pszVkwBQwrUV+DbfoHHIZFplAKFwE6/xA2zPPibUWpiHRSggDUEGKCsMY/sggKGEFD7onw+PCAlLk12QRJ4pKwYt4gX4Ut2LRyfAhSwngADlPXmlB1RIOkC9ZX4sl9iNYAlSSjmJThwX9nreCcJY3NIClDAJgIMUDaZaLZJgWQI1J6Nrwg/rpHAhfEeXwDPSon1ZdV4I95j8foUoAAFGKD4HqAABeIusHsp5np9uBQCX43xYw9ek8AfHH4859qKg3FvhANQgAIUOC7AAMW3AgUokFCBPRWY6RVYColFUuA0qIdx6tl4LtAiJLZLgXekxFa/E6+evAWHE1o8B6MABSjAAMX3AAUoYBSBTxeh1JmKUp/EBIdErgAypEC3BNqcDjSlCewtYVgyynSxDgpQAABXoPg2oAAFKEABClCAAhEKMEBFCMbDKUABClCAAhSgAAMU3wMUoAAFKEABClAgQgEGqAjBeDgFKEABClCAAhRggOJ7gAIUoAAFKEABCkQowAAVIRgPpwAFKEABClCAAgxQfA9QgAIUoAAFKECBCAUYoCIE4+EUoAAFKEABClCAAYrvAQpQgAIUoAAFKBChAANUhGA8nAIUoAAFKEABCvw/jH+P6V9fhVwAAAAASUVORK5CYII=";
        byte[] bytes = Convert.FromBase64String(base64String);
        Image image;
        using (MemoryStream ms = new MemoryStream(bytes))
        {
            image = Image.FromStream(ms);
        }
        var imageLocation = @"1.jpeg";
        image.Save(imageLocation, System.Drawing.Imaging.ImageFormat.Jpeg);

“data:image/png;base64,” is not base 64.

so which api of gojs should i use to get base64 data of my model diagram ?

You got it already. https://tools.ietf.org/html/rfc2397

OK, so after passing only the Base64string, still getting the same error:

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.

Here is the code:

var base64String = “iVBORw0KGgoAAAANSUhEUgAAAlAAAALSCAYAAAASmu+WAAAgAElEQVR4XuxdB5Qc1bG9PZtz0OaglbSSEMoC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IMECZZaZYJwUMJrD7HEz3+vBHSHzOYKWNVs5LZR583UT1slQKUMCgAgxQBp0YlkUBMwjsPx8nHe3BbwGcb4J6N5R58F0T1MkSKUABEwgwQJlgklgiBYwuUFuJ9ZC4xqh1CoFbXVV40Kj1sS4KUMB8AgxQ5pszVkwBQwrUV+DbfoHHIZFplAKFwE6/xA2zPPibUWpiHRSggDUEGKCsMY/sggKGEFD7onw+PCAlLk12QRJ4pKwYt4gX4Ut2LRyfAhSwngADlPXmlB1RIOkC9ZX4sl9iNYAlSSjmJThwX9nreCcJY3NIClDAJgIMUDaZaLZJgWQI1J6Nrwg/rpHAhfEeXwDPSon1ZdV4I95j8foUoAAFGKD4HqAABeIusHsp5np9uBQCX43xYw9ek8AfHH4859qKg3FvhANQgAIUOC7AAMW3AgUokFCBPRWY6RVYColFUuA0qIdx6tl4LtAiJLZLgXekxFa/E6+evAWHE1o8B6MABSjAAMX3AAUoYBSBTxeh1JmKUp/EBIdErgAypEC3BNqcDjSlCewtYVgyynSxDgpQAABXoPg2oAAFKEABClCAAhEKMEBFCMbDKUABClCAAhSgAAMU3wMUoAAFKEABClAgQgEGqAjBeDgFKEABClCAAhRggOJ7gAIUoAAFKEABCkQowAAVIRgPpwAFKEABClCAAgxQfA9QgAIUoAAFKECBCAUYoCIE4+EUoAAFKEABClCAAYrvAQpQgAIUoAAFKBChAANUhGA8nAIUoAAFKEABCvw/jH+P6V9fhVwAAAAASUVORK5CYII=”;
byte[] bytes = Convert.FromBase64String(base64String);