{"record":{"id":"2f6ce94f310a4406","repo":"JosefNemec/Playnite","slug":"the-icon-is-corrupt-couldn-t-read-the-header","errorCode":null,"errorMessage":"The icon is corrupt. Couldn't read the header.","messagePattern":"The icon is corrupt\\. Couldn't read the header\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"warning","filePath":"source/Playnite/Common/Media/Icons/IconUtil.cs","lineNumber":182,"sourceCode":"                    case 3:\n                        return data[46];\n                    case 4:\n                        return data[46] * 2;\n                    case 6:\n                        return data[46] * 4;\n                    default:\n                        // NOP\n                        break;\n                }\n            }\n            else if (data.Length >= 22)\n            {\n                // The picture is not PNG. Read ICONDIRENTRY structure.\n\n                return BitConverter.ToUInt16(data, 12);\n            }\n\n            throw new ArgumentException(\"The icon is corrupt. Couldn't read the header.\", \"icon\");\n        }\n\n        private static byte[] GetIconData(Icon icon)\n        {\n            var data = getIconData(icon);\n            if (data != null)\n            {\n                return data;\n            }\n            else\n            {\n                using (var ms = new MemoryStream())\n                {\n                    icon.Save(ms);\n                    return ms.ToArray();\n                }\n            }\n        }","sourceCodeStart":164,"sourceCodeEnd":200,"githubUrl":"https://github.com/JosefNemec/Playnite/blob/5911f4e964e628aa7a69c2030ab35101afa63067/source/Playnite/Common/Media/Icons/IconUtil.cs#L164-L200","documentation":"Thrown by IconUtil when byte data passed to read an icon is neither a recognized PNG header nor large enough to read an ICONDIRENTRY (data.Length < 22 or an unrecognized structure). The argument name is 'icon'; it signals the icon bytes are malformed/truncated.","triggerScenarios":"Passing a corrupt, zero-byte, or truncated icon file/byte array; passing bytes that are not an icon at all (e.g. a JPEG mislabeled .ico); a downloaded icon whose transfer was interrupted leaving < 22 bytes.","commonSituations":"Fetching a game icon from a metadata provider that returned an HTML error page instead of image bytes; reading a .ico while it was being rewritten; user-supplied icon file that is actually a renamed PNG/JPEG with an unreadable header.","solutions":["Validate data.Length >= 22 and sniff the PNG/ICO magic bytes before calling the converter.","Re-download the icon and verify Content-Type / length matches an image.","Fall back to a default icon when the source cannot be parsed.","Check the source file is a genuine .ico and not a renamed JPEG/PNG."],"exampleFix":"// before\nvar size = IconUtil.GetIconSize(bytes);\n\n// after — sniff magic bytes before parsing\nif (bytes == null || bytes.Length < 22) return null;\nbool isPng = bytes.Length >= 8 && bytes[0] == 0x89 && bytes[1] == 'P';\nif (!isPng && BitConverter.ToUInt16(bytes, 0) != 0) return null; // not ICO\nvar size = IconUtil.GetIconSize(bytes);","handlingStrategy":"validation","validationCode":"if (data == null || data.Length < 22) return null;\nbool isPng = data.Length >= 8 && data[0] == 0x89 && data[1] == (byte)'P' && data[2] == (byte)'N' && data[3] == (byte)'G';\nif (!isPng && BitConverter.ToUInt16(data, 0) != 0) return null; // not an ICO","typeGuard":"static bool LooksLikeIcon(byte[] d) => d != null && d.Length >= 22 &&\n    (IsPng(d) || BitConverter.ToUInt16(d, 0) == 0);","tryCatchPattern":"try { return IconUtil.GetIconSize(bytes); }\ncatch (ArgumentException) { return null; /* corrupt icon — fall back to default */ }","preventionTips":["Sniff PNG/ICO magic bytes before parsing.","Verify downloaded icon Content-Type and length.","Fall back to a default icon on parse failure."],"tags":["media","icon","validation","playnite"],"backgroundTag":null,"analyzedSha":"5911f4e964e628aa7a69c2030ab35101afa63067","analyzedAt":"2026-08-13T17:38:25.713Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}