question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IImageLoadingService cannot load image on Windows

See original GitHub issue

Description

What I was trying to achieve:

  • resize an image selected from PC using MAUI on Windows

Result:

  • I didn’t get past loading the file as IImage

Sample application:

MauiApp2 Android IImage resized image disposed exception.zip (yes, the same as for https://github.com/dotnet/maui/issues/6909)

Description

Any attempt to load an image using IImageLoadingService on Windows results in this code being called: image

"No resource creator has been registered globally or for this thread."

Exception thrown: 'System.Exception' in Microsoft.Maui.Graphics.Win2D.WinUI.Desktop.dll
An exception of type 'System.Exception' occurred in Microsoft.Maui.Graphics.Win2D.WinUI.Desktop.dll but was not handled in user code
No resource creator has been registered globally or for this thread.

How I am calling it:

image

imageLoadingService.FromStream which is Microsoft.Maui.Graphics.Win2D.SkiaImageLoadingService which is the only IImageLoadingService implementation which works for me for Windows (during compile-time).

Win2DImage cannot be called directly because it is an internal class.

What I am doing in MauiProgram.cs:

#if WINDOWS
            services.AddSingleton<IImageLoadingService, Microsoft.Maui.Graphics.Win2D.SkiaImageLoadingService>();
#endif
#if ANDROID
            services.AddSingleton<IImageLoadingService, Microsoft.Maui.Graphics.Platform.PlatformImageLoadingService>();
#endif

Steps to Reproduce

Inject Microsoft.Maui.Graphics.Win2D.SkiaImageLoadingService for IImageLoadingService on Windows and try creating an IImage from file/stream with it.

Version with bug

Release Candidate 2 (current)

Last version that worked well

Unknown/Other

Affected platforms

Windows

Affected platform versions

I don’t know.

Did you find any workaround?

No response

Relevant log output

No response

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:6
  • Comments:7

github_iconTop GitHub Comments

3reactions
limefrogyankcommented, May 26, 2022

I found a workaround. Put a dummy GraphicsView on the page somewhere where it loads first and handle the Loaded event with this:

private void dummy_Loaded(object sender, EventArgs e)
    {
#if WINDOWS
        Assembly assembly = typeof(Microsoft.Maui.Graphics.Win2D.W2DCanvas).Assembly;
        var type = assembly.GetType("Microsoft.Maui.Graphics.Win2D.W2DGraphicsService");
        var prop = type.GetProperty("GlobalCreator");

        var graphicsView = (GraphicsView)sender;
        var view = (Microsoft.Maui.Platform.PlatformTouchGraphicsView)graphicsView.Handler.PlatformView;
        var view2 = (Microsoft.Maui.Graphics.Win2D.W2DGraphicsView)view.Content;
        prop.SetValue(null, view2.Content);
#endif

    }

I actually get a COM error the first time I try to load an image, but after the first one (handled via try…catch) it works fine. All of the images load. Maybe there’s a timing issue still, but it works for me… at least to get to the next stage of working on my app.

1reaction
limefrogyankcommented, May 26, 2022

This is just to load an stream into an IImage. The error you get comes from the GlobalCreator property being always null. Once you populate it with an actual CanvasControl from Win2D, then the loading a stream into an IImage works.

My dummy GraphicsView is 1 pixel by 1 pixel and it’s covered by other controls. The windows version of GraphicsView uses a CanvasControl internally.

Somewhere later in my code, I’m doing this to load an image into an IImage:

var service = new Microsoft.Maui.Graphics.Win2D.W2DImageLoadingService();
IImage _image = service.FromStream(_ms.AsStream(), ImageFormat.Jpeg);

Again, I do get an initial COM error when trying the above code, but it works eventually. (My app is loading several pages from a pdf file as images and displaying them. If it can’t get the image, it tries again at the next draw call when the GraphicsView is Invalidated. So this works for me…)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Windows 10 in app images failing to load
So here's the procedure: Test a new account by creating a new Local Admin account in Settings > Accounts > Family & Other...
Read more >
Recognize .NET MAUI Image loading failure - Microsoft Q&A
I'm using the .NET MAUI Image control to display images stored on disk. In rare cases an image file can be damaged, so...
Read more >
c# - .NET 6/7 Migration from System.Drawing.Common to ...
I like to migrate my application to Microsoft.Maui.Graphics (as Microsoft suggests on their breaking changes page for .NET 6/7). They support ...
Read more >
Solved: Photos Cannot Preview or Open in Windows
Methods to fix Photos Cannot Preview or Open in Windows Error · 1. Update Windows Photo Viewer · 2. Open photos in different...
Read more >
How to fix 'Windows Photo Viewer can't open this picture' ...
This post shares how you can fix 'Windows Photo Viewer can't open this picture' problem and view your photos on Windows PC.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found