iosdeveloper
iosdeveloper

Reputation: 215

Recognize .NET MAUI Image loading failure

I'm using the .NET MAUI Image control to display images stored on disk.

In rare cases an image file can be damaged, so the image cannot be loaded into the Image control. If this happens, I see an exception in the log:

Microsoft.Maui.FileImageSourceService: Warning: Unable to load image file '/Users/frank/Library/c12d2fe8-90bb-4a19-9887-6091059ea74d-2023-02-20T10-44-41-855Z'.

System.InvalidOperationException: Unable to load image file. at Microsoft.Maui.FileImageSourceService.GetImageAsync(IFileImageSource imageSource, Single scale, CancellationToken cancellationToken)

I would like to display a special warning symbol if loading fails.

How can I catch this exception or get to know in any other way, if loading has succeeded or failed?

Upvotes: 2

Views: 3375

Answers (2)

iosdeveloper
iosdeveloper

Reputation: 215

Thanks for the hint in Q&A, the Handler.MauiContext in the second solution can be taken from the ContentPage for example. My problem here was that the image is located inside a DataTemplate for a CollectionView.

I've slightly changed the second solution, because using the Loaded event sometimes didn't work. Instead I've used the property change for IsLoading, which is fired every time the CollectionView cell becomes visible during scrolling.

Besides this, I had to catch the exception instead of checking res to be null.

So this is my working code now:

image.PropertyChanged += async (object sender, PropertyChangedEventArgs eargs) => {

    if (eargs.PropertyName == "IsLoading") {

        if (!image.IsLoading) {

            var mauiContext = /* get ContentPage.Handler.MauiContext */;

            try {

                var res = await image.Source.GetPlatformImageAsync(mauiContext);
            }
            catch (Exception e) {

                Debug.WriteLine($"EXCEPTION CAUGHT {e}");

                image.Source = "error.png";
            }
        }
    }
};

Upvotes: 3

Zack
Zack

Reputation: 1629

You can try to use try...catch to catch exceptions to check whether the loading is successful, refer to the following:

public TestPageOne()
{
    var image = new Image();
 
    Content = new VerticalStackLayout
    {
       Children = {
                     new Label { HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.Center, Text = "Welcome to .NET MAUI!"
                   },
                     image
                 }
             };
 
       try
           {
              image.Source = "one.png";
                       
           }catch(Exception ex)
           {
              DisplayAlert("error",ex.Message,"OK");
           }
    
}

I noticed that you also raised this question on the Q&A(Recognize .NET MAUI Image loading failure). You can also refer to the GetPlatformImageAsync method to determine whether the returned result is empty to determine whether the image is loaded successfully. The reference is as follows:

<Image x:Name="img"
    Loaded="Image_Loaded"
    Source="dotnet.png"
    ...
    />
private async void Image_Loaded(object sender, EventArgs e)

{
    var res = await img.Source.GetPlatformImageAsync(Handler.MauiContext);
    if (res == null)
    {
        img.Source = "dotnet_bot.png";
    }
}

Upvotes: 0

Related Questions