{"record":{"id":"ebe0e3493b8396ad","repo":"LykosAI/StabilityMatrix","slug":"failed-to-get-bitmap-from-image","errorCode":null,"errorMessage":"Failed to get bitmap from image","messagePattern":"Failed to get bitmap from image","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Avalonia/Services/InferenceClientManager.cs","lineNumber":764,"sourceCode":"                );\n                using var stream = new MemoryStream(bytes);\n\n                await Client.UploadImageAsync(stream, uploadName, cancellationToken);\n            }\n            else\n            {\n                await using var stream = localFile.Info.OpenRead();\n\n                await Client.UploadImageAsync(stream, uploadName, cancellationToken);\n            }\n        }\n        else\n        {\n            logger.LogDebug(\"Uploading bitmap as {UploadName}\", uploadName);\n\n            if (await image.GetBitmapAsync() is not { } bitmap)\n            {\n                throw new InvalidOperationException(\"Failed to get bitmap from image\");\n            }\n\n            await using var ms = new MemoryStream();\n            bitmap.Save(ms);\n            ms.Position = 0;\n\n            await Client.UploadImageAsync(ms, uploadName, cancellationToken);\n        }\n    }\n\n    /// <inheritdoc />\n    public async Task UploadMaskImageAsync(\n        SkiaSharp.SKImage maskImage,\n        string fileName,\n        CancellationToken cancellationToken = default\n    )\n    {\n        EnsureConnected();","sourceCodeStart":746,"sourceCodeEnd":782,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Avalonia/Services/InferenceClientManager.cs#L746-L782","documentation":"Thrown when converting an image into a bitmap for upload fails: IImage.GetBitmapAsync() returns null. The manager cannot encode the image to the MemoryStream that follows, so it aborts the upload early with an explicit InvalidOperationException instead of a NullReferenceException later.","triggerScenarios":"Passing an image (e.g. from clipboard, file, or a render surface) whose GetBitmapAsync() yields null into UploadInputImageAsync/UploadMaskImageAsync — typically an image backed by an unsupported or already-released pixel buffer.","commonSituations":"Uploading a clipboard image with no bitmap data; image loaded from a stream that failed decode; using an image object after its underlying surface was disposed; platform-specific bitmap format not supported by Avalonia/Skia.","solutions":["Verify the source image decoded successfully (check pixel dimensions are > 0) before uploading","Reload the image from its original file/stream to get a fresh decodable bitmap","Convert the image to a supported format (e.g. new Bitmap(stream)) before passing it in","Check the image source (clipboard/file) actually contained valid image data"],"exampleFix":"// before\nawait clientManager.UploadInputImageAsync(maybeImage, \"input.png\");\n// after\nusing var bitmap = maybeImage.GetBitmapAsync().Result ?? throw new InvalidOperationException(\"Image has no bitmap data\");\nusing var fresh = new Bitmap(bitmap.SaveToStream()); // ensure decodable copy\nawait clientManager.UploadInputImageAsync(fresh, \"input.png\");","handlingStrategy":"validation","validationCode":"var bmp = await image.GetBitmapAsync();\nif (bmp is null || bmp.PixelSize.Width == 0)\n    throw new InvalidOperationException(\"Image contains no decodable bitmap\");","typeGuard":"async Task<bool> HasBitmapAsync(IImage image) => await image.GetBitmapAsync() is not null;","tryCatchPattern":"try\n{\n    await clientManager.UploadInputImageAsync(image, \"input.png\");\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Failed to get bitmap\"))\n{\n    logger.LogWarning(\"Source image was not decodable; re-encode before upload\");\n}","preventionTips":["Validate images decode (non-zero pixel size) before upload","Re-load images from a stream rather than reusing disposed surfaces","Prefer File/BitmapSource images over clipboard bitmaps with unknown backing data"],"tags":["image","bitmap","upload","null"],"backgroundTag":"null-argument","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"}