LykosAI/StabilityMatrix · error · ArgumentException

Must have at least one image

Error message

Must have at least one image

What it means

CreateImageGrid composites a list of SKImages into a grid and cannot do so with zero images: it indexes images[0] for dimensions and computes grid dimensions from the count. An empty list is treated as a programming/caller error and ArgumentException is thrown immediately.

Solutions

  1. Check images.Count > 0 before calling CreateImageGrid and skip/skip-and-log the grid step when empty.
  2. Fix the upstream image loading/filtering so at least one image reaches the grid step.
  3. For UI grids, show an empty-state placeholder instead of attempting to compose a grid.

Example fix

// before
var grid = ImageProcessor.CreateImageGrid(images);
// after
var grid = images.Count > 0 ? ImageProcessor.CreateImageGrid(images) : null;
Defensive patterns

Strategy: validation

Validate before calling

if (images is null || images.Count == 0)
    return null; // or show empty state

Type guard

static bool HasImages(IReadOnlyList<SKImage> imgs) => imgs is { Count: > 0 };

Try / catch

try { grid = ImageProcessor.CreateImageGrid(images); }
catch (ArgumentException ex) when (ex.Message == "Must have at least one image")
{ grid = null; ShowEmptyState(); }

Prevention

When it happens

Trigger: Calling ImageProcessor.CreateImageGrid with an empty IReadOnlyList<SKImage> — e.g. a preview grid built from a directory that yielded no decodable images, or a filter that removed all images before the grid step.

Common situations: Empty output folder or failed image loads when creating preview grids; caller passes a filtered list that ends up empty; test code constructs the grid before seeding images.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of LykosAI/StabilityMatrix@af93d6ef57 (2026-09-12). Data as JSON: /api/errors/51bd9d5cc1aab2f9. Report an issue: GitHub.

Appendix: source

Thrown at StabilityMatrix.Avalonia/Helpers/ImageProcessor.cs:32

    public static (int rows, int columns) GetGridDimensionsFromImageCount(int count)
    {
        if (count <= 1)
            return (1, 1);
        if (count == 2)
            return (1, 2);

        // Prefer one extra row over one extra column,
        // the row count will be the floor of the square root
        // and the column count will be floor of count / rows
        var rows = (int)Math.Floor(Math.Sqrt(count));
        var columns = (int)Math.Floor((double)count / rows);
        return (rows, columns);
    }

    public static SKImage CreateImageGrid(IReadOnlyList<SKImage> images, int spacing = 0)
    {
        if (images.Count == 0)
            throw new ArgumentException("Must have at least one image");

        var (rows, columns) = GetGridDimensionsFromImageCount(images.Count);

        var singleWidth = images[0].Width;
        var singleHeight = images[0].Height;

        // Make output image
        using var output = new SKBitmap(
            singleWidth * columns + spacing * (columns - 1),
            singleHeight * rows + spacing * (rows - 1)
        );

        // Draw images
        using var canvas = new SKCanvas(output);

        foreach (
            var (row, column) in Enumerable.Range(0, rows).Product(Enumerable.Range(0, columns))
        )

View on GitHub (pinned to af93d6ef57)