HandyOrg/HandyControl · error · ArgumentException

InvalidFrame

Error message

InvalidFrame

What it means

The GifImageInfo.Frame property setter validates the requested frame index against FrameCount and throws ArgumentException("InvalidFrame") when the value is negative or >= FrameCount. The GIF decoder in HandyControl only exposes frames that actually exist in the source image, so any out-of-range index is rejected before mutating internal state.

Solutions

  1. Clamp the value before assigning: frame = Math.Max(0, Math.Min(value, info.FrameCount - 1)).
  2. Ensure the GIF metadata is loaded (FrameCount > 0) before setting Frame; for non-animated GIFs do not set Frame at all.
  3. Use 0-based indexing: the first frame is 0, the last is FrameCount - 1.

Example fix

// before
gifInfo.Frame = gifInfo.FrameCount; // last frame?
// after
if (gifInfo.FrameCount > 0)
    gifInfo.Frame = gifInfo.FrameCount - 1;
Defensive patterns

Strategy: validation

Validate before calling

if (gifInfo != null && frameIndex >= 0 && frameIndex < gifInfo.FrameCount)
    gifInfo.Frame = frameIndex;

Type guard

static bool IsValidFrame(HandyControl.Data.Gif.GifImageInfo info, int frame) => info != null && info.FrameCount > 0 && frame >= 0 && frame < info.FrameCount;

Try / catch

try { gifInfo.Frame = requestedFrame; } catch (ArgumentException ex) when (ex.Message == "InvalidFrame") { gifInfo.Frame = 0; // safe reset
}

Prevention

When it happens

Trigger: Setting GifImageInfo.Frame to a negative number, or to a value equal to or greater than FrameCount (e.g. Frame = FrameCount, forgetting indices are 0-based).

Common situations: Programmatically seeking an animated GIF to its last frame using a 1-based index; caching FrameCount before the GIF metadata finished loading (FrameCount still 0); data-binding a frame slider whose maximum is off by one.

Related errors


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

Appendix: source

Thrown at src/Shared/HandyControl_Shared/Data/Gif/GifImageInfo.cs:36

    public bool Animated { get; }

    public EventHandler FrameChangedHandler { get; set; }

    internal int FrameTimer { get; set; }

    public bool FrameDirty { get; private set; }

    public int Frame
    {
        get => _frame;
        set
        {
            if (_frame != value)
            {
                if (value < 0 || value >= FrameCount)
                {
                    throw new ArgumentException("InvalidFrame");
                }

                if (Animated)
                {
                    _frame = value;
                    FrameDirty = true;

                    OnFrameChanged(EventArgs.Empty);
                }
            }
        }
    }

    public GifImageInfo(GifImage image)
    {
        Image = image;
        Animated = ImageAnimator.CanAnimate(image);

View on GitHub (pinned to 2c0875ebd6)