{"record":{"id":"3524facfe9868da3","repo":"d2phap/ImageGlass","slug":"the-base64-content-is-invalid-3524fa","errorCode":null,"errorMessage":"The base64 content is invalid.","messagePattern":"The base64 content is invalid\\.","errorType":"exception","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"v9/Components/ImageGlass.Base/BHelper/Photoing.cs","lineNumber":432,"sourceCode":"    /// Converts base64 string to byte array, returns MIME type and raw data in byte array.\n    /// </summary>\n    /// <param name=\"content\">Base64 string</param>\n    /// <returns></returns>\n    public static (string MimeType, byte[] ByteData) ConvertBase64ToBytes(string? content)\n    {\n        if (string.IsNullOrWhiteSpace(content))\n        {\n            throw new ArgumentNullException(nameof(content));\n        }\n\n        // data:image/svg-xml;base64,xxxxxxxx\n        // type is optional\n        var base64DataUri = Base64DataUriRegex();\n\n        var match = base64DataUri.Match(content);\n        if (!match.Success)\n        {\n            throw new FormatException(\"The base64 content is invalid.\");\n        }\n\n\n        var base64Data = match.Groups[\"data\"].Value;\n        var byteData = Convert.FromBase64String(base64Data);\n        var mimeType = match.Groups[\"type\"].Value.ToLowerInvariant();\n\n        if (mimeType.Length == 0)\n        {\n            // use default PNG MIME type\n            mimeType = \"image/png\";\n        }\n\n        return (mimeType, byteData);\n    }\n\n\n    /// <summary>","sourceCodeStart":414,"sourceCodeEnd":450,"githubUrl":"https://github.com/d2phap/ImageGlass/blob/4a3c4feceffc5a8bb5e56ba836509634aaae47a9/v9/Components/ImageGlass.Base/BHelper/Photoing.cs#L414-L450","documentation":"Thrown by BHelper.ConvertBase64ToBytes when the input does not match the data-URI regex (^data:(?<type>image/[a-z+\\-]*);base64,)?(?<data>[a-zA-Z0-9+/=]+)$ (case-insensitive, anchored). The MIME prefix is optional, so a bare base64 string is accepted, but any byte outside the base64 alphabet — interior whitespace, newlines, URL-safe chars '-', '_', or a missing/invalid data: prefix — fails the match and raises FormatException. A prior ArgumentNullException is thrown only when the content is null/whitespace, so this error specifically means 'non-empty but malformed'.","triggerScenarios":"Passing a base64 data URI with interior whitespace or line breaks; using URL-safe base64 ('-','_') instead of standard ('+','/'); supplying a 'data:image/svg+xml;utf8,' (non-base64) URI; truncating the data portion; including a BOM or stray quote characters.","commonSituations":"Reading base64 from a JSON/theme file that was pretty-printed with wrapped lines; copy-paste introducing spaces; SVG encoded as 'utf8' instead of 'base64'; a theme/clipboard payload encoded by a tool that emits URL-safe base64.","solutions":["Strip all whitespace and newlines from the content before calling: content = new string(content.Where(c => !char.IsWhiteSpace(c)).ToArray()).","If you have URL-safe base64, convert it first: replace '-' with '+' and '_' with '/' before passing.","Ensure the prefix, if present, is exactly 'data:image/<sub>;base64,' and that the data is standard-alphabet base64 with valid padding.","Validate against the same regex (or try Convert.FromBase64String in a sandbox) before calling to produce a clearer upstream error."],"exampleFix":"// before\nvar (mime, bytes) = BHelper.ConvertBase64ToBytes(raw);\n\n// after: sanitize whitespace + url-safe -> standard base64\nvar cleaned = new string(raw.Where(c => !char.IsWhiteSpace(c)).ToArray())\n    .Replace('-', '+').Replace('_', '/');\nvar (mime, bytes) = BHelper.ConvertBase64ToBytes(cleaned);","handlingStrategy":"validation","validationCode":"// Sanitize to standard base64 with no whitespace before converting.\nvar cleaned = new string(content.Where(c => !char.IsWhiteSpace(c)).ToArray())\n    .Replace('-', '+').Replace('_', '/');\nif (!System.Text.RegularExpressions.Regex.IsMatch(\n        cleaned,\n        @\"(^data:image/[a-z+\\-]*;base64,)?[a-zA-Z0-9+/=]+$\",\n        RegexOptions.IgnoreCase))\n    throw new FormatException(\"Content is not valid base64 / data URI.\");","typeGuard":null,"tryCatchPattern":"try { var (mime, bytes) = BHelper.ConvertBase64ToBytes(cleaned); }\ncatch (FormatException) { /* not a base64 data URI */ }","preventionTips":["Strip whitespace/newlines from base64 before passing.","Convert URL-safe base64 ('-','_') to standard ('+','/') first.","Ensure any 'data:' prefix uses ';base64,' not ';utf8,'."],"tags":["base64","data-uri","regex","formatexception","parsing","imageglass","v9"],"backgroundTag":null,"analyzedSha":"4a3c4feceffc5a8bb5e56ba836509634aaae47a9","analyzedAt":"2026-08-13T16:58:15.523Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}