Forum Xamarin.Forms

Image and ImageSource.FromUri - where is the error returned if the download failed?

KarhuKotiKarhuKoti USMember ✭✭

Hi,

We are downloading images from the internet with a Uri which is passed into ImageSource.

This generally works, but sometimes it doesn't on various .png or .jpg images.

So, how do these controls return the error that was encountered?

Br, Karhu.

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Personally I wouldn't rely on the control for that level of robustness.
    Download the image once in its own call - that you can monitor and get errors from.
    Store it locally on the device. Now you don't need to download it 20 times. You can even work off-line.
    Put it in the Image from the locally saved file.

  • KarhuKotiKarhuKoti USMember ✭✭

    Do you have an example of how to properly download the file?

  • AlenTomaAlenToma USMember ✭✭

    Here is how you could download the image from url

     public static System.IO.MemoryStream GetImage(string filename)
        {
    
            System.Net.WebClient webClient = new System.Net.WebClient();
            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            webClient.OpenRead(filename).CopyTo(stream);
            return stream;
    
        }
    
Sign In or Register to comment.