shimat / opencvsharp_blazor_sample

OpenCvSharp + Blazor WebAssembly Code Samples
Apache License 2.0
12 stars 2 forks source link

Failed to load image files #2

Open yushulx opened 2 years ago

yushulx commented 2 years ago

I tried to use InputFile to load an image, but failed to load PNG and JPEG files.

Here is the code that I have added to OpenCvSharpSample.razor:

<InputFile OnChange="LoadImage" />

protected async Task LoadImage(InputFileChangeEventArgs e)
    {
        var imageFile = e.File;
        var jsImageStream = imageFile.OpenReadStream(1024 * 1024 * 20);

        try {
            using (MemoryStream ms = new MemoryStream()) {
                await jsImageStream.CopyToAsync(ms);
                var imageData = ms.ToArray();
                srcMat ??= Mat.FromImageData(imageData);
                Console.WriteLine($"Loaded image: {srcMat.Width}x{srcMat.Height}");

                srcCanvasClient ??= new CanvasClient(jsRuntime, srcCanvas);
                dstCanvasClient ??= new CanvasClient(jsRuntime, dstCanvas);

                await srcCanvasClient.DrawMatAsync(srcMat);
            }
        }
        catch (Exception ex) {
            Console.WriteLine(ex);
        }
        finally {
            jsImageStream.Dispose();
        }

    }

The Mandrill.bmp file can be loaded successfully. However, after converting the image to PNG, I got an empty Mat.

Screenshot 2022-04-26 204420

Does OpenCVSharp for blazor only support bmp format?

kikipoulet commented 2 years ago

Heavily interested in this issue too, I can't upload any image with blazor webassembly and draw it in canvas :(

shimat commented 2 years ago

I too am having trouble with this issue 🤔 I am not familiar with (blazor) wasm. The sample program here is experimental.