Skip to content

Conversation

@MartyIX
Copy link
Contributor

@MartyIX MartyIX commented Jul 16, 2024

Description of Change

It appears that on Windows, it's quite slow load many images using PlatformImage.FromStream because it uses an AsyncPump implementation.

I tested with the same batch of images and the load times differ significantly for me:

  • main - 9 seconds
  • PR - 1.3 seconds

TODO: Measure in Release configuration.

Issues Fixed

Fixes #

@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Jul 16, 2024
@dotnet-policy-service
Copy link
Contributor

Hey there @MartyIX! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@MartyIX MartyIX marked this pull request as ready for review July 16, 2024 10:38
@MartyIX MartyIX requested a review from a team as a code owner July 16, 2024 10:38
@MartyIX
Copy link
Contributor Author

MartyIX commented Jul 16, 2024

This won't be added because the benefit is too small for new API.

@MartyIX MartyIX closed this Jul 16, 2024
@MartyIX MartyIX deleted the feature/2024-07-16-Add-PlatformImage.FromStreamAsync branch July 16, 2024 14:56
@MartyIX
Copy link
Contributor Author

MartyIX commented Jul 16, 2024

A workaround is:

using System.Threading;
using Microsoft.Graphics.Canvas;

// ...

#if WINDOWS
    [ThreadStatic]
    private static ICanvasResourceCreator? Creator;

    private static async Task<IImage> PlatformImageFromStreamAsync(Stream stream, ImageFormat format = ImageFormat.Png)
    {
        Creator ??= CanvasDevice.GetSharedDevice();

        if (Creator is null)
            throw new Exception("No resource creator has been registered globally or for this thread.");

        CanvasBitmap bitmap = await CanvasBitmap.LoadAsync(Creator, stream.AsRandomAccessStream());
        return new PlatformImage(Creator, bitmap);
    }
#else
    private static Task<IImage> PlatformImageFromStreamAsync(Stream stream, ImageFormat format = ImageFormat.Png)
        => Task.FromResult(PlatformImage.FromStream(stream, format));
#endif

@bronteq
Copy link

bronteq commented Jul 16, 2024

Try to propose anyway, maybe can be merged for .Net9

@MartyIX
Copy link
Contributor Author

MartyIX commented Jul 16, 2024

I have already discussed it with .NET MAUI developers. They don't find sufficient justification for adding new API at the moment.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

community ✨ Community Contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants