HandyOrg/HandyControl · error · Exception

ImageCodecInfo is null

Error message

ImageCodecInfo is null

What it means

Guard in ScreenshotWindow thrown when the GDI+ image-encoder lookup returns no codecs (ImageCodecInfo lookup yields null), so the captured screenshot cannot be encoded to the requested format. It fires when GdipGetImageEncodersSize/GetImageEncoders finds no registered encoder for the requested mime type on the machine.

Solutions

  1. Check the encoder array for null/empty before use and pick the first available encoder instead of assuming a specific codec
  2. Fall back to saving the captured bitmap as PNG (GDI+ built-in) when the requested encoder is unavailable
  3. Verify the mime type string passed to the encoder lookup is a well-known value (image/png, image/jpeg)
Defensive patterns

Strategy: fallback

When it happens

Trigger: Thrown at src/Shared/HandyControl_Shared/Controls/Screenshot/ScreenshotWindow.cs:582 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of HandyOrg/HandyControl@2c0875ebd6 (2026-09-14). Data as JSON: /api/errors/ed0c1e15bb3c6d7e. Report an issue: GitHub.

Appendix: source

Thrown at src/Shared/HandyControl_Shared/Controls/Screenshot/ScreenshotWindow.cs:582

        InteropMethods.SelectObject(hdcDest, hOld);
        InteropMethods.DeleteDC(hdcDest);
        InteropMethods.ReleaseDC(_desktopWindowHandle, hdcSrc);

        var status = InteropMethods.Gdip.GdipCreateBitmapFromHBITMAP(new HandleRef(null, hbitmap), new HandleRef(null, IntPtr.Zero), out var bitmap);
        if (status != InteropMethods.Gdip.Ok) throw InteropMethods.Gdip.StatusException(status);

        using var ms = new MemoryStream();
        status = InteropMethods.Gdip.GdipGetImageEncodersSize(out var numEncoders, out var size);
        if (status != InteropMethods.Gdip.Ok) throw InteropMethods.Gdip.StatusException(status);

        var memory = Marshal.AllocHGlobal(size);
        try
        {
            status = InteropMethods.Gdip.GdipGetImageEncoders(numEncoders, size, memory);
            if (status != InteropMethods.Gdip.Ok) throw InteropMethods.Gdip.StatusException(status);

            var codecInfo = ImageCodecInfo.ConvertFromMemory(memory, numEncoders).FirstOrDefault(item => item.FormatID.Equals(BmpGuid));
            if (codecInfo == null) throw new Exception("ImageCodecInfo is null");

            var encoderParamsMemory = IntPtr.Zero;

            try
            {
                var g = codecInfo.Clsid;
                status = InteropMethods.Gdip.GdipSaveImageToStream(new HandleRef(this, bitmap),
                    new InteropValues.ComStreamFromDataStream(ms), ref g,
                    new HandleRef(null, encoderParamsMemory));
            }
            finally
            {
                if (encoderParamsMemory != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(encoderParamsMemory);
                }
            }

View on GitHub (pinned to 2c0875ebd6)