{"record":{"id":"ce62ff7b3baf403f","repo":"JosefNemec/Playnite","slug":"cannot-convert-value-to-aspectratio","errorCode":null,"errorMessage":"Cannot convert {value} to AspectRatio.","messagePattern":"Cannot convert (.+?) to AspectRatio\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"warning","filePath":"source/Playnite/Common/Sizes.cs","lineNumber":34,"sourceCode":"        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)\r\n        {\r\n            if (value is string ratio)\r\n            {\r\n                var regex = Regex.Match(ratio, @\"(\\d+):(\\d+)\");\r\n                if (regex.Success)\r\n                {\r\n                    return new AspectRatio(\r\n                        Convert.ToInt32(regex.Groups[1].Value),\r\n                        Convert.ToInt32(regex.Groups[2].Value));\r\n                }\r\n                else\r\n                {\r\n                    // For cases where this is called from designer element preview via binding expression\r\n                    return new AspectRatio();\r\n                }\r\n            }\r\n\r\n            throw new NotSupportedException($\"Cannot convert {value} to AspectRatio.\");\r\n        }\r\n\r\n        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)\r\n        {\r\n            return sourceType == typeof(string);\r\n        }\r\n    }\r\n\r\n    [TypeConverter(typeof(AspectRatioTypeConverter))]\r\n    public class AspectRatio : IEquatable<AspectRatio>\r\n    {\r\n        public int Width { get; set; }\r\n        public int Height { get; set; }\r\n\r\n        public AspectRatio()\r\n        {\r\n        }\r\n\r","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/JosefNemec/Playnite/blob/5911f4e964e628aa7a69c2030ab35101afa63067/source/Playnite/Common/Sizes.cs#L16-L52","documentation":"Thrown by AspectRatioTypeConverter.ConvertFrom when the supplied value cannot be parsed into an AspectRatio. The converter accepts a 'W:H' or 'WxH' style string (regex groups) and falls back to a default for designer previews; any other shape/value reaches the throw as NotSupportedException.","triggerScenarios":"Binding a non-string value, or a string that does not match the expected W:H regex (e.g. '16/9', '1.77', 'wide', empty, or a decimal ratio); passing a null outside the designer path.","commonSituations":"User/theme config with a malformed aspect ratio string; a plugin/theme binding the wrong type; localized config using a different separator; designer hitting the converter with an unexpected value.","solutions":["Provide the ratio in the supported 'W:H' format (e.g. '16:9', '4:3').","Validate the string against the converter's regex before binding.","Ensure the bound value is a string and not null or a numeric type."],"exampleFix":"// before\nvar ar = (AspectRatio)new AspectRatioTypeConverter().ConvertFrom(input);\n\n// after — validate the ratio string first\nstatic bool TryRatio(string s, out int w, out int h)\n{\n    w = h = 0;\n    var m = System.Text.RegularExpressions.Regex.Match(s ?? \"\", @\"^(\\d+)\\s*[:xX]\\s*(\\d+)$\");\n    return m.Success && int.TryParse(m.Groups[1].Value, out w) && int.TryParse(m.Groups[2].Value, out h);\n}\nif (!TryRatio(input, out var w, out var h)) { ar = new AspectRatio(); }\nelse { ar = new AspectRatio(w, h); }","handlingStrategy":"validation","validationCode":"static bool TryParseRatio(string s, out int w, out int h)\n{\n    w = h = 0;\n    var m = System.Text.RegularExpressions.Regex.Match(s ?? \"\", @\"^(\\d+)\\s*[:xX]\\s*(\\d+)$\");\n    return m.Success && int.TryParse(m.Groups[1].Value, out w) && int.TryParse(m.Groups[2].Value, out h);\n}","typeGuard":"static bool IsValidRatioString(string s, out int w, out int h) => TryParseRatio(s, out w, out h);","tryCatchPattern":"try { return (AspectRatio)new AspectRatioTypeConverter().ConvertFrom(input); }\ncatch (NotSupportedException) { return new AspectRatio(); /* default */ }","preventionTips":["Always format ratios as 'W:H' (e.g. '16:9').","Validate the string with the converter's regex before binding.","Ensure the bound value is a non-null string."],"tags":["ui","type-converter","validation","playnite"],"backgroundTag":null,"analyzedSha":"5911f4e964e628aa7a69c2030ab35101afa63067","analyzedAt":"2026-08-13T17:38:25.713Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}