d2phap/ImageGlass · error · FormatException
The base64 content is invalid.
Error message
The base64 content is invalid.
What it means
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'.
Source
Thrown at v9/Components/ImageGlass.Base/BHelper/Photoing.cs:432
/// Converts base64 string to byte array, returns MIME type and raw data in byte array.
/// </summary>
/// <param name="content">Base64 string</param>
/// <returns></returns>
public static (string MimeType, byte[] ByteData) ConvertBase64ToBytes(string? content)
{
if (string.IsNullOrWhiteSpace(content))
{
throw new ArgumentNullException(nameof(content));
}
// data:image/svg-xml;base64,xxxxxxxx
// type is optional
var base64DataUri = Base64DataUriRegex();
var match = base64DataUri.Match(content);
if (!match.Success)
{
throw new FormatException("The base64 content is invalid.");
}
var base64Data = match.Groups["data"].Value;
var byteData = Convert.FromBase64String(base64Data);
var mimeType = match.Groups["type"].Value.ToLowerInvariant();
if (mimeType.Length == 0)
{
// use default PNG MIME type
mimeType = "image/png";
}
return (mimeType, byteData);
}
/// <summary>View on GitHub (pinned to 4a3c4fecef)
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.
Example fix
// before
var (mime, bytes) = BHelper.ConvertBase64ToBytes(raw);
// after: sanitize whitespace + url-safe -> standard base64
var cleaned = new string(raw.Where(c => !char.IsWhiteSpace(c)).ToArray())
.Replace('-', '+').Replace('_', '/');
var (mime, bytes) = BHelper.ConvertBase64ToBytes(cleaned); Defensive patterns
Strategy: validation
Validate before calling
// Sanitize to standard base64 with no whitespace before converting.
var cleaned = new string(content.Where(c => !char.IsWhiteSpace(c)).ToArray())
.Replace('-', '+').Replace('_', '/');
if (!System.Text.RegularExpressions.Regex.IsMatch(
cleaned,
@"(^data:image/[a-z+\-]*;base64,)?[a-zA-Z0-9+/=]+$",
RegexOptions.IgnoreCase))
throw new FormatException("Content is not valid base64 / data URI."); Try / catch
try { var (mime, bytes) = BHelper.ConvertBase64ToBytes(cleaned); }
catch (FormatException) { /* not a base64 data URI */ } Prevention
- Strip whitespace/newlines from base64 before passing.
- Convert URL-safe base64 ('-','_') to standard ('+','/') first.
- Ensure any 'data:' prefix uses ';base64,' not ';utf8,'.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- The base64 content is invalid.
- IGE_001
- IGE_002
- Cannot open registry key: {key}
- Unable to load '{themeFolderName}' theme pack. Please make s
AI-assisted analysis of d2phap/ImageGlass@4a3c4fecef (2026-08-13).
Data as JSON: /api/errors/3524facfe9868da3.
Report an issue: GitHub.