{"record":{"id":"0d5fdde47ad4e3a8","repo":"HandyOrg/HandyControl","slug":"statusexception-status-imagecodecinfo","errorCode":null,"errorMessage":"StatusException(status)","messagePattern":"StatusException\\(status\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Shared/HandyControl_Shared/Data/Info/ImageCodecInfo.cs","lineNumber":62,"sourceCode":"    public string MimeType { get; set; }\n\n    public ImageCodecFlags Flags { get; set; }\n\n    public int Version { get; set; }\n\n    public byte[][] SignaturePatterns { get; set; }\n\n    public byte[][] SignatureMasks { get; set; }\n\n    public static ImageCodecInfo[] GetImageDecoders()\n    {\n        ImageCodecInfo[] imageCodecs;\n\n        var status = InteropMethods.Gdip.GdipGetImageDecodersSize(out var numDecoders, out var size);\n\n        if (status != InteropMethods.Gdip.Ok)\n        {\n            throw InteropMethods.Gdip.StatusException(status);\n        }\n\n        var memory = Marshal.AllocHGlobal(size);\n\n        try\n        {\n            status = InteropMethods.Gdip.GdipGetImageDecoders(numDecoders, size, memory);\n\n            if (status != InteropMethods.Gdip.Ok)\n            {\n                throw InteropMethods.Gdip.StatusException(status);\n            }\n\n            imageCodecs = ConvertFromMemory(memory, numDecoders);\n        }\n        finally\n        {\n            Marshal.FreeHGlobal(memory);","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/HandyOrg/HandyControl/blob/2c0875ebd67326e0c67282967e3e809c69282fee/src/Shared/HandyControl_Shared/Data/Info/ImageCodecInfo.cs#L44-L80","documentation":"GetImageDecoders calls GdipGetImageDecodersSize and throws a StatusException when the returned GDI+ status is not Ok. This wraps native enumeration failure — GDI+ could not report how many decoders exist or the buffer size needed.","triggerScenarios":"Calling ImageCodecInfo.GetImageDecoders when GdipGetImageDecodersSize fails, which happens if GDI+ is not initialized or native GDI+ is in a bad state.","commonSituations":"Probing supported image formats on a machine with a broken/patched gdiplus.dll, or calling codec enumeration before GdiplusStartup completed.","solutions":["Check the status code in the exception to identify the GDI+ failure (e.g. NotInitialized).","Verify GDI+ startup succeeded before enumerating decoders.","Repair the system GDI+ installation (sfc /scannow) if failures are machine-specific.","Cache codec lists at startup so a transient failure does not hit every decode call."],"exampleFix":"// before\nvar status = InteropMethods.Gdip.GdipGetImageDecodersSize(out var numDecoders, out var size);\nif (status != InteropMethods.Gdip.Ok)\n{\n    throw InteropMethods.Gdip.StatusException(status);\n}\n// after\nvar status = InteropMethods.Gdip.GdipGetImageDecodersSize(out var numDecoders, out var size);\nif (status != InteropMethods.Gdip.Ok)\n{\n    throw InteropMethods.Gdip.StatusException(status, $\"GetImageDecodersSize failed (status={status}); ensure GDI+ is initialized\");\n}","handlingStrategy":"try-catch","validationCode":"if (!InteropMethods.Gdip.ApplicationStarted)\n{\n    throw new InvalidOperationException(\"GDI+ must be initialized before enumerating decoders.\");\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    var decoders = ImageCodecInfo.GetImageDecoders();\n}\ncatch (StatusException ex)\n{\n    logger.Error($\"Decoder enumeration failed: {ex.Status}\");\n    decoders = Array.Empty<ImageCodecInfo>(); // safe fallback\n}","preventionTips":["Enumerate codecs once at startup and cache the result.","Only call after successful GDI+ startup.","Wrap native enumeration calls in try/catch with a cached fallback list."],"tags":["gdiplus","image-codec","native-interop"],"backgroundTag":"gdiplus-status-error","analyzedSha":"2c0875ebd67326e0c67282967e3e809c69282fee","analyzedAt":"2026-09-14T14:45:29.754Z","contentChangedAt":"2026-09-14T14:45:29.754Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}