{"record":{"id":"8f2981f4c18bd976","repo":"HandyOrg/HandyControl","slug":"unable-to-initialize-gdi","errorCode":null,"errorMessage":"Unable to initialize GDI+","messagePattern":"Unable to initialize GDI\\+","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"critical","filePath":"src/Shared/Microsoft.Windows.Shell/Standard/SafeGdiplusStartupToken.cs","lineNumber":35,"sourceCode":"        Status status = NativeMethods.GdiplusShutdown(this.handle);\n        return status == Status.Ok;\n    }\n\n    [SuppressMessage(\"Microsoft.Performance\", \"CA1811:AvoidUncalledPrivateCode\")]\n    [SuppressMessage(\"Microsoft.Reliability\", \"CA2000:Dispose objects before losing scope\")]\n    [SuppressMessage(\"Microsoft.Usage\", \"CA2201:DoNotRaiseReservedExceptionTypes\")]\n    public static SafeGdiplusStartupToken Startup()\n    {\n        SafeGdiplusStartupToken safeGdiplusStartupToken = new SafeGdiplusStartupToken();\n        IntPtr handle;\n        StartupOutput startupOutput;\n        if (NativeMethods.GdiplusStartup(out handle, new StartupInput(), out startupOutput) == Status.Ok)\n        {\n            safeGdiplusStartupToken.handle = handle;\n            return safeGdiplusStartupToken;\n        }\n        safeGdiplusStartupToken.Dispose();\n        throw new Exception(\"Unable to initialize GDI+\");\n    }\n}\n","sourceCodeStart":17,"sourceCodeEnd":38,"githubUrl":"https://github.com/HandyOrg/HandyControl/blob/2c0875ebd67326e0c67282967e3e809c69282fee/src/Shared/Microsoft.Windows.Shell/Standard/SafeGdiplusStartupToken.cs#L17-L38","documentation":"SafeGdiplusStartupToken.Startup calls GdiplusStartup; if the returned Status is not Ok the token is disposed and a bare Exception('Unable to initialize GDI+') is thrown, since all GDI+ drawing depends on a successful startup token.","triggerScenarios":"Calling SafeGdiplusStartupToken.Startup when GdiplusStartup returns a non-Ok Status — e.g. GdiplusNotInitialized, OutOfMemory, or UnsupportedGdiplusVersion — typically when gdiplus.dll is missing or too old.","commonSituations":"Running on stripped-down/Server Core Windows installs without GDI+, corrupted system libraries, or unusual remote-session environments.","solutions":["Verify gdiplus.dll is present and a supported version on the machine","Check the returned startupOutput.Status value for the specific GDI+ failure reason","Ensure the app targets a supported Windows version / install the platform update","Catch the exception and disable GDI+-dependent features gracefully"],"exampleFix":"// before\nvar token = SafeGdiplusStartupToken.Startup(); // throws bare Exception on old systems\n// after\nSafeGdiplusStartupToken token = null;\ntry { token = SafeGdiplusStartupToken.Startup(); }\ncatch (Exception) { /* fallback: skip GDI+ rendering */ }","handlingStrategy":"try-catch","validationCode":"static bool GdiPlusAvailable() =>\n    NativeMethods.LoadLibrary(\"gdiplus.dll\") != IntPtr.Zero;","typeGuard":"static bool IsGdiplusTokenValid(SafeGdiplusStartupToken t) => t != null && !t.IsInvalid;","tryCatchPattern":"try { token = SafeGdiplusStartupToken.Startup(); }\ncatch (Exception ex) { Log.Error(\"GDI+ startup failed\", ex); DisableRenderingFeatures(); }","preventionTips":["Verify gdiplus.dll presence/version on deployment targets","Fail fast at startup so GDI+ problems surface before feature use","Keep the token alive for the process lifetime; dispose only at shutdown"],"tags":["gdiplus","initialization","win32","native"],"backgroundTag":"module-init-failed","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"}