LykosAI/StabilityMatrix · error · InvalidOperationException

Failed to get bitmap from image

Error message

Failed to get bitmap from image

What it means

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.

Solutions

  1. Verify the source image decoded successfully (check pixel dimensions are > 0) before uploading
  2. Reload the image from its original file/stream to get a fresh decodable bitmap
  3. Convert the image to a supported format (e.g. new Bitmap(stream)) before passing it in
  4. Check the image source (clipboard/file) actually contained valid image data

Example fix

// before
await clientManager.UploadInputImageAsync(maybeImage, "input.png");
// after
using var bitmap = maybeImage.GetBitmapAsync().Result ?? throw new InvalidOperationException("Image has no bitmap data");
using var fresh = new Bitmap(bitmap.SaveToStream()); // ensure decodable copy
await clientManager.UploadInputImageAsync(fresh, "input.png");
Defensive patterns

Strategy: validation

Validate before calling

var bmp = await image.GetBitmapAsync();
if (bmp is null || bmp.PixelSize.Width == 0)
    throw new InvalidOperationException("Image contains no decodable bitmap");

Type guard

async Task<bool> HasBitmapAsync(IImage image) => await image.GetBitmapAsync() is not null;

Try / catch

try
{
    await clientManager.UploadInputImageAsync(image, "input.png");
}
catch (InvalidOperationException ex) when (ex.Message.Contains("Failed to get bitmap"))
{
    logger.LogWarning("Source image was not decodable; re-encode before upload");
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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

Appendix: source

Thrown at StabilityMatrix.Avalonia/Services/InferenceClientManager.cs:764

                );
                using var stream = new MemoryStream(bytes);

                await Client.UploadImageAsync(stream, uploadName, cancellationToken);
            }
            else
            {
                await using var stream = localFile.Info.OpenRead();

                await Client.UploadImageAsync(stream, uploadName, cancellationToken);
            }
        }
        else
        {
            logger.LogDebug("Uploading bitmap as {UploadName}", uploadName);

            if (await image.GetBitmapAsync() is not { } bitmap)
            {
                throw new InvalidOperationException("Failed to get bitmap from image");
            }

            await using var ms = new MemoryStream();
            bitmap.Save(ms);
            ms.Position = 0;

            await Client.UploadImageAsync(ms, uploadName, cancellationToken);
        }
    }

    /// <inheritdoc />
    public async Task UploadMaskImageAsync(
        SkiaSharp.SKImage maskImage,
        string fileName,
        CancellationToken cancellationToken = default
    )
    {
        EnsureConnected();

View on GitHub (pinned to af93d6ef57)