LykosAI/StabilityMatrix · error · InvalidOperationException

ImageSource is not a local file or bitmap

Error message

ImageSource is not a local file or bitmap

What it means

GetHashGuidFileNameCached() can hash either a local file's bytes or an in-memory Bitmap's bytes; it needs one of the two to compute a Blake3 content hash. If neither LocalFile nor Bitmap is set, it throws InvalidOperationException because no data source exists to derive the GUID from.

Solutions

  1. Initialize the ImageSource with either a local file path or a Bitmap before hashing.
  2. If starting from a URL, download to a local file or load a Bitmap first.
  3. Guard the call site: only invoke GetHashGuidFileNameCached when LocalFile is not null or Bitmap is not null.
  4. Validate inputs at construction time to avoid half-initialized ImageSource objects.

Example fix

// before
var img = new ImageSource(); // never initialized
var name = img.GetHashGuidFileNameCached();
// after
var img = new ImageSource(bitmap: myBitmap);
var name = img.GetHashGuidFileNameCached();
Defensive patterns

Strategy: type-guard

Validate before calling

if (img.LocalFile is null && img.Bitmap is null)
    throw new InvalidOperationException("ImageSource has no file or bitmap");

Type guard

bool IsHashable(ImageSource s) => s.LocalFile is not null || s.Bitmap is not null;

Try / catch

try { name = img.GetHashGuidFileNameCached(); }
catch (InvalidOperationException) { name = Guid.NewGuid().ToString(); } // unhashable placeholder

Prevention

When it happens

Trigger: Calling GetHashGuidFileNameCached() on a default/empty ImageSource where both LocalFile and Bitmap are null (or only a remote URL was set without materializing a local file or bitmap).

Common situations: Constructing ImageSource() without assigning an image source; deserializing a partial ImageSource; passing an ImageSource initialized only from a URL into hash-dependent APIs.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

Thrown at StabilityMatrix.Avalonia/Models/ImageSource.cs:250

                {
                    throw new FileNotFoundException("Image file does not exist", LocalFile);
                }

                // Fail in debug since hash should have been pre-calculated
                Debug.Fail("Hash has not been calculated when GetHashGuidFileNameCached() was called");

                var data = LocalFile.ReadAllBytes();
                contentHashBlake3 = FileHash.GetBlake3Parallel(data);
            }
            // Bitmap
            else if (Bitmap is not null)
            {
                var data = Bitmap.ToByteArray();
                contentHashBlake3 = FileHash.GetBlake3Parallel(data);
            }
            else
            {
                throw new InvalidOperationException("ImageSource is not a local file or bitmap");
            }
        }

        var guid = contentHashBlake3.Value.ToGuid().ToString();

        if (LocalFile?.Extension is { } extension)
        {
            guid += extension;
        }
        else
        {
            // Default to PNG if no extension
            guid += ".png";
        }

        return guid;
    }

View on GitHub (pinned to af93d6ef57)