AvaloniaUI/Avalonia · error · FormatException
Invalid PixelSize.
Error message
Invalid PixelSize.
What it means
Thrown by PixelSize.Parse when TryParse fails to interpret the input string. PixelSize expects a 'width,height' or 'widthxheight' formatted string of two non-negative integers. If TryParse returns false (malformed, non-numeric, or negative components), Parse wraps the failure in this FormatException.
Source
Thrown at src/Avalonia.Base/PixelSize.cs:78
/// <returns>True if the sizes are unequal; otherwise false.</returns>
public static bool operator !=(PixelSize left, PixelSize right)
{
return !(left == right);
}
/// <summary>
/// Parses a <see cref="PixelSize"/> string.
/// </summary>
/// <param name="s">The string.</param>
/// <returns>The <see cref="PixelSize"/>.</returns>
/// <exception cref="FormatException"/>
public static PixelSize Parse(string s)
{
if (TryParse(s, out var result))
{
return result;
}
throw new FormatException("Invalid PixelSize.");
}
/// <summary>
/// Try parsing <paramref name="source"/> as <see cref="PixelSize"/>.
/// </summary>
/// <param name="source">The <see cref="string"/> to parse.</param>
/// <param name="result">The result of parsing. if <paramref name="source"/> is not valid <paramref name="result"/> is <see cref="PixelSize.Empty"/> </param>
/// <returns><c>true</c> if <paramref name="source"/> is valid <see cref="PixelSize"/>, otherwise <c>false</c>.</returns>
public static bool TryParse([NotNullWhen(true)] string? source,
out PixelSize result)
{
result = Empty;
if(string.IsNullOrEmpty(source))
{
return false;
}
using (var tokenizer = new SpanStringTokenizer(source, exceptionMessage: "Invalid PixelSize."))
{View on GitHub (pinned to 11c5427268)
Solutions
- Format the string as two non-negative integers separated by a comma, e.g. '1920,1080'.
- Prefer PixelSize.TryParse to handle invalid input gracefully without exceptions.
- Construct PixelSize directly from ints when the values are available programmatically.
Example fix
// before
var size = PixelSize.Parse("1920x1080x extra"); // throws
// after
var size = PixelSize.Parse("1920,1080");
// or safe parse:
if (PixelSize.TryParse(input, out var size)) { /* use size */ } Defensive patterns
Strategy: validation
Validate before calling
static PixelSize SafeParsePixelSize(string s)
=> PixelSize.TryParse(s, out var size) ? size : PixelSize.Empty; Try / catch
try { return PixelSize.Parse(s); }
catch (FormatException) { return PixelSize.Empty; } Prevention
- Prefer TryParse for user-supplied or external size strings.
- Format as two non-negative integers separated by a comma.
- Construct PixelSize directly from ints when values are known.
When it happens
Trigger: Calling PixelSize.Parse with a string that is not two non-negative integers in the expected delimiter form, e.g. 'abc', '10', '10x-5', or '10;20'. TryParse returns false and Parse throws.
Common situations: Deserializing a size from config/XAML with the wrong delimiter or missing a dimension; user input that omits one component; locale-specific separators (comma vs. period); negative dimensions.
Related errors
- Invalid text trimming string: '{s}'.
- Invalid transform string: '{s}'.
- Too many provided values.
- Invalid value {value.Value} {unitString} for {function}
- Invalid format. {function} expects {count} value(s).
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/7a6ddc1282a94822.
Report an issue: GitHub.