d2phap/ImageGlass · warning · ArgumentException

Wallpaper style is not valid.

Error message

Wallpaper style is not valid.

What it means

Thrown in igcmd Functions.SetDesktopWallpaper when Enum.TryParse(styleStr, out WallpaperStyle) fails. WallpaperStyle is an int enum: Current=-1, Centered=0, Stretched=1, Tiled=2; Enum.TryParse is case-sensitive and parses by name (or a numeric string that fits the int), so any other token fails. It is an ArgumentException whose paramName is the offending styleStr itself. The throw happens inside Functions.Run, which catches it, sets IgExitCode.Error, and (if Program.ShowUi) shows the message via Config.ShowError.

Source

Thrown at v9/igcmd/Functions.cs:54

    /// </summary>
    /// <param name="imgPath">Full path of image file</param>
    /// <param name="styleStr">Wallpaper style, see <see cref="WallpaperStyle"/>.</param>
    public static IgExitCode SetDesktopWallpaper(string imgPath, string styleStr)
    {
        return Run(() =>
        {
            if (Enum.TryParse(styleStr, out WallpaperStyle style))
            {
                var exception = DesktopApi.SetWallpaper(imgPath, style);

                if (exception != null)
                {
                    throw exception;
                }
            }
            else
            {
                throw new ArgumentException("Wallpaper style is not valid.", styleStr);
            }

        }, (error) =>
        {
            _ = Config.ShowError(null,
                description: error.Message,
                title: Config.Language["FrmSettings._Theme._UninstallTheme"]);
        });
    }


    /// <summary>
    /// Sets or unsets app extensions
    /// </summary>
    /// <param name="enable"></param>
    /// <param name="exts">Extensions to proceed. Example: <c>.png;.jpg;</c></param>
    public static IgExitCode SetAppExtensions(bool enable, string exts = "", bool perMachine = false)
    {

View on GitHub (pinned to 4a3c4fecef)

Solutions

  1. Pass one of the exact enum names: 'Current', 'Centered', 'Stretched', or 'Tiled' (case-sensitive).
  2. If accepting user input, normalize it to the enum name before calling SetDesktopWallpaper (e.g. map 'tile' -> 'Tiled').
  3. Validate styleStr against the allowed set before invoking igcmd and surface a clear error at the caller.
  4. Prefer passing the numeric value through the same channel only if it is in range [-1,2].

Example fix

// before
return (int)Functions.SetDesktopWallpaper(CmdArgs[1], CmdArgs[2]);

// after: whitelist + case-insensitive normalize before calling
static readonly HashSet<string> ValidStyles =
    new(StringComparer.OrdinalIgnoreCase) { "Current", "Centered", "Stretched", "Tiled" };

var rawStyle = CmdArgs[2];
if (!ValidStyles.Contains(rawStyle))
{
    Console.Error.WriteLine($"Invalid wallpaper style '{rawStyle}'. Use one of: {string.Join(", ", ValidStyles)}.");
    return (int)IgExitCode.Error;
}
// Enum.TryParse below is case-sensitive, so pass the canonical-cased name
var canonical = ValidStyles.First(s => s.Equals(rawStyle, StringComparison.OrdinalIgnoreCase));
return (int)Functions.SetDesktopWallpaper(CmdArgs[1], canonical);
Defensive patterns

Strategy: validation

Validate before calling

// Whitelist and normalize the wallpaper style before calling SetDesktopWallpaper
static readonly HashSet<string> ValidWallpaperStyles =
    new(StringComparer.OrdinalIgnoreCase) { "Current", "Centered", "Stretched", "Tiled" };

static bool TryNormalizeWallpaperStyle(string raw, out string canonical)
{
    canonical = ValidWallpaperStyles.FirstOrDefault(s => s.Equals(raw, StringComparison.OrdinalIgnoreCase));
    return canonical != null;
}

Type guard

static bool IsValidWallpaperStyle(string style) =>
    style != null && ValidWallpaperStyles.Contains(style);  // case-insensitive set

Try / catch

// SetDesktopWallpaper already wraps the action in Functions.Run and shows Config.ShowError.
// Treat a non-zero exit code (IgExitCode.Error) as the user-facing signal:
var code = Functions.SetDesktopWallpaper(imgPath, styleStr);
if (code == IgExitCode.Error)
{
    logger.Warn("Invalid wallpaper style '{0}'.", styleStr);
}

Prevention

When it happens

Trigger: igcmd is invoked with the wallpaper command and the third command-line argument (styleStr) is not one of 'Current'/'Centered'/'Stretched'/'Tiled' (nor a numeric equivalent). Examples: lowercase 'tiled', a localized word, an out-of-range number, or a typo like 'Streched'.

Common situations: A user or script passes a localized style name; the calling UI sends the wrong casing; the arg is omitted and replaced by an unrelated token; or a config file stores the style under a different vocabulary than the enum names.

Related errors


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