Avalonia icon indicating copy to clipboard operation
Avalonia copied to clipboard

Bitmap has format null when created from Grayscale image

Open MoHamzawy opened this issue 1 year ago • 0 comments

Describe the bug

I am working on a project where we use OpenCvSharp for handling images in the backend. For the frontend, we use Bitmap. I have created a simple converter as follows:

public static Bitmap? MatToBitmap(Mat image)
{
    // Try catch here in case Mat is disposed from UI thread.
    try
    {
        byte[] imageData = image.ImEncode(".png");
        using (var stream = new MemoryStream(imageData))
        {
            var bitmap = new Bitmap(stream);
            return bitmap;
        }
    }
    catch(Exception ex)
    {
        XLog.Error(ex.Message, "Failed to convert Mat to Bitmap");
        return null;
    }
}

The problem here is that if mat has 1 channel (gray scale) bitmap.Format is null

To Reproduce

Running the code above shows the problem.

Expected behavior

No response

Avalonia version

11.1.2

OS

No response

Additional context

No response

MoHamzawy avatar Aug 13 '24 15:08 MoHamzawy