d2phap/ImageGlass Β· error Β· ArgumentException

'{modeStr}' is not a valid zoom mode. ---------- πŸ‘‰πŸΌ Metho

Error message

'{modeStr}' is not a valid zoom mode.

----------
πŸ‘‰πŸΌ Method: IG_SetZoomMode

What it means

Thrown by the string overload of IG_SetZoomMode when Enum.TryParse<ZoomMode>(modeStr) fails. ZoomMode is case-insensitively parsed; valid names are AutoZoom, LockZoom, ScaleToWidth, ScaleToHeight, ScaleToFit, ScaleToFill. Any other string raises ArgumentException.

Source

Thrown at source/ImageGlass.Lib/Common/ServiceProviders/AppAPIs/AppAPIProvider.cs:1247

        if (Viewer.ZoomFactor < 1)
        {
            IG_SetZoom(1);
        }
        else
        {
            IG_Refresh();
        }
    }


    /// <summary>
    /// Sets the zoom mode value.
    /// </summary>
    public void IG_SetZoomMode(string? modeStr)
    {
        if (!Enum.TryParse<ZoomMode>(modeStr, out var mode))
        {
            throw new ArgumentException($"""
                '{modeStr}' is not a valid zoom mode.

                ----------
                πŸ‘‰πŸΌ Method: {nameof(IG_SetZoomMode)}
                """,
                nameof(modeStr));
        }

        IG_SetZoomMode(mode);
    }

    /// <summary>
    /// Sets the zoom mode value.
    /// </summary>
    public static void IG_SetZoomMode(ZoomMode mode)
    {
        if (mode == Core.Config.ZoomMode)
        {

View on GitHub (pinned to 4a3c4fecef)

Solutions

  1. Use one of the exact enum names: AutoZoom, LockZoom, ScaleToWidth, ScaleToHeight, ScaleToFit, ScaleToFill.
  2. Validate with Enum.TryParse<ZoomMode> upstream and pass the enum to the typed overload.
  3. Trim whitespace and map user aliases to the canonical name before calling.

Example fix

// before
api.IG_SetZoomMode(userInput);

// after
if (Enum.TryParse<ZoomMode>(userInput?.Trim(), true, out var mode))
{
    api.IG_SetZoomMode(mode);
}
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.TryParse<ZoomMode>(modeStr?.Trim(), true, out var mode))
    throw new ArgumentException($"Unknown zoom mode '{modeStr}'.");
api.IG_SetZoomMode(mode);

Type guard

static bool IsValidZoomMode(string? s) => Enum.TryParse<ZoomMode>(s?.Trim(), true, out _);

Try / catch

try { api.IG_SetZoomMode(modeStr); }
catch (ArgumentException ex) when (ex.ParamName == nameof(modeStr))
{ /* valid: AutoZoom, LockZoom, ScaleToWidth, ScaleToHeight, ScaleToFit, ScaleToFill */ }

Prevention

When it happens

Trigger: Calling IG_SetZoomMode(string) with a value not in the enum: 'fit' (use 'ScaleToFit'), 'lock' (use 'LockZoom'), 'fill', 'auto', or a numeric string (Enum.TryParse accepts numeric strings for the underlying int, but a non-numeric label that is not a member name fails).

Common situations: User types a short alias instead of the full enum name; config authored with a legacy or v9 name; casing or a trailing space in the value.

Related errors


AI-assisted analysis of d2phap/ImageGlass@4a3c4fecef (2026-08-13). Data as JSON: /api/errors/c5a4e49646f3fd2d. Report an issue: GitHub.