{"record":{"id":"68167ef0fa76c641","repo":"LykosAI/StabilityMatrix","slug":"imagesource-is-not-a-local-file-or-bitmap","errorCode":null,"errorMessage":"ImageSource is not a local file or bitmap","messagePattern":"ImageSource is not a local file or bitmap","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Avalonia/Models/ImageSource.cs","lineNumber":250,"sourceCode":"                {\n                    throw new FileNotFoundException(\"Image file does not exist\", LocalFile);\n                }\n\n                // Fail in debug since hash should have been pre-calculated\n                Debug.Fail(\"Hash has not been calculated when GetHashGuidFileNameCached() was called\");\n\n                var data = LocalFile.ReadAllBytes();\n                contentHashBlake3 = FileHash.GetBlake3Parallel(data);\n            }\n            // Bitmap\n            else if (Bitmap is not null)\n            {\n                var data = Bitmap.ToByteArray();\n                contentHashBlake3 = FileHash.GetBlake3Parallel(data);\n            }\n            else\n            {\n                throw new InvalidOperationException(\"ImageSource is not a local file or bitmap\");\n            }\n        }\n\n        var guid = contentHashBlake3.Value.ToGuid().ToString();\n\n        if (LocalFile?.Extension is { } extension)\n        {\n            guid += extension;\n        }\n        else\n        {\n            // Default to PNG if no extension\n            guid += \".png\";\n        }\n\n        return guid;\n    }\n","sourceCodeStart":232,"sourceCodeEnd":268,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Avalonia/Models/ImageSource.cs#L232-L268","documentation":"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.","triggerScenarios":"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).","commonSituations":"Constructing ImageSource() without assigning an image source; deserializing a partial ImageSource; passing an ImageSource initialized only from a URL into hash-dependent APIs.","solutions":["Initialize the ImageSource with either a local file path or a Bitmap before hashing.","If starting from a URL, download to a local file or load a Bitmap first.","Guard the call site: only invoke GetHashGuidFileNameCached when LocalFile is not null or Bitmap is not null.","Validate inputs at construction time to avoid half-initialized ImageSource objects."],"exampleFix":"// before\nvar img = new ImageSource(); // never initialized\nvar name = img.GetHashGuidFileNameCached();\n// after\nvar img = new ImageSource(bitmap: myBitmap);\nvar name = img.GetHashGuidFileNameCached();","handlingStrategy":"type-guard","validationCode":"if (img.LocalFile is null && img.Bitmap is null)\n    throw new InvalidOperationException(\"ImageSource has no file or bitmap\");","typeGuard":"bool IsHashable(ImageSource s) => s.LocalFile is not null || s.Bitmap is not null;","tryCatchPattern":"try { name = img.GetHashGuidFileNameCached(); }\ncatch (InvalidOperationException) { name = Guid.NewGuid().ToString(); } // unhashable placeholder","preventionTips":["Always construct ImageSource via constructors that take a file or bitmap.","Centralize ImageSource creation so uninitialized instances cannot escape.","Add debug asserts at creation time that LocalFile or Bitmap is set."],"tags":["invalid-state","image","initialization"],"backgroundTag":"invalid-state-transition","analyzedSha":"af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002","analyzedAt":"2026-09-12T19:02:43.389Z","contentChangedAt":"2026-09-12T19:02:43.389Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}