NickeManarin/ScreenToGif · error · Exception

It was not possible to move the current selected region to t

Error message

It was not possible to move the current selected region to the closest monitor.

What it means

MoveToClosestScreen finds the monitor whose Name matches Screen.FromRectangle(...).DeviceName, falling back to the first monitor. If _viewModel.Monitors is empty (or no entry matches and FirstOrDefault is null), it throws — there is no monitor to relocate the region onto.

Source

Thrown at ScreenToGif/Windows/NewRecorder.xaml.cs:1668

    /// <summary>
    /// Move the selection region to the closest screen when outside of any.
    /// </summary>
    private void MoveToClosestScreen()
    {
        //If the position was never set.
        if (_viewModel.Region.IsEmpty)
            return;

        var top = _viewModel.Region.Top;
        var left = _viewModel.Region.Left;

        var screen = Screen.FromRectangle(new Rectangle((int)_viewModel.Region.Left, (int)_viewModel.Region.Top, (int)_viewModel.Region.Width, (int)_viewModel.Region.Height));
        var closest = _viewModel.Monitors.FirstOrDefault(f => f.Name == screen.DeviceName) ?? _viewModel.Monitors.FirstOrDefault();
        //var closest = monitors.FirstOrDefault(x => x.Bounds.Contains(new System.Windows.Point((int)left, (int)top))) ?? monitors.FirstOrDefault(x => x.IsPrimary) ?? monitors.FirstOrDefault();

        if (closest == null)
            throw new Exception("It was not possible to move the current selected region to the closest monitor.");

        //To much to the Left.
        if (closest.Bounds.Left > _viewModel.Region.Left - 1)
            left = closest.Bounds.Left;

        //Too much to the top.
        if (closest.Bounds.Top > _viewModel.Region.Top - 1)
            top = closest.Bounds.Top;

        //Too much to the right.
        if (closest.Bounds.Right < _viewModel.Region.Left + _viewModel.Region.Width)
            left = closest.Bounds.Right - _viewModel.Region.Width;

        //Too much to the bottom.
        if (closest.Bounds.Bottom < _viewModel.Region.Top + _viewModel.Region.Height)
            top = closest.Bounds.Bottom - _viewModel.Region.Height;

        UserSettings.All.SelectedRegionScale = closest.Scale;

View on GitHub (pinned to a4d0a67c21)

Solutions

  1. Re-enumerate _viewModel.Monitors before throwing so a freshly-attached display is detected.
  2. Reconnect at least one display before moving the region.
  3. If no monitor is available, gracefully reset _viewModel.Region to empty instead of throwing.
  4. Trigger MoveToClosestScreen only after a confirmed Monitors refresh.

Example fix

// before
var closest = _viewModel.Monitors.FirstOrDefault(f => f.Name == screen.DeviceName) ?? _viewModel.Monitors.FirstOrDefault();
if (closest == null)
    throw new Exception("It was not possible to move the current selected region to the closest monitor.");

// after
var closest = _viewModel.Monitors.FirstOrDefault(f => f.Name == screen.DeviceName) ?? _viewModel.Monitors.FirstOrDefault();
if (closest == null)
    throw new Exception("It was not possible to move the current selected region to the closest monitor. No display detected — reconnect a monitor and retry.");
Defensive patterns

Strategy: validation

Validate before calling

if (_viewModel.Monitors == null || _viewModel.Monitors.Count == 0)
{
    await MonitorEnumerator.EnumerateAsync();
    if (_viewModel.Monitors.Count == 0) return; // nothing to move to
}

Type guard

bool HasAnyMonitor => _viewModel.Monitors?.Any() == true;

Try / catch

try { MoveToClosestScreen(); }
catch (Exception ex) when (ex.Message.Contains("closest monitor"))
{ /* reset _viewModel.Region to empty or warn user to reconnect a display */ }

Prevention

When it happens

Trigger: _viewModel.Region is non-empty AND (_viewModel.Monitors.FirstOrDefault(name == screen.DeviceName) ?? _viewModel.Monitors.FirstOrDefault()) returns null. The Monitors collection is empty at the moment of the call.

Common situations: A monitor was disconnected while the recorder was open and Monitors was cleared during the display-change refresh; multi-monitor reconfiguration event emptied the collection; rapid dock/undock of a laptop left the cache cold.

Related errors


AI-assisted analysis of NickeManarin/ScreenToGif@a4d0a67c21 (2026-08-13). Data as JSON: /api/errors/138a096d045c66fb. Report an issue: GitHub.