Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Image.Source as string

It's easy to set a source for an image in Xamarin:

using Xamarin.Forms;
Image image = new Image;
image.Source = "someImage.jpg";

But I can not do the reverse operation.
ex: given an image with its source already set, print the source.

Console.WriteLine ("Print Image source ==> {0}",image.Source);
Console.WriteLine ("Print Image source ==> {0}",image.Source.ToString());

... and a few more incoherent combinations.

Could anyone tell me how to get the source (with a string) from an image.

like image 405
Loebre Avatar asked Oct 30 '14 17:10

Loebre


1 Answers

The Xamarin.Forms Image.Source property is of type ImageSource.

ImageSource in Xamarin.Forms has a few classes that inherit this class such as:-

  • FileImageSource
  • StreamImageSource
  • UriImageSource

You can type check the Image.Source to see what implementation is being used in Image.Source, and then cast it, and access the properties of the casted object.

For instance (assuming ImageSource is a FileImageSource) you will have something like:-

Xamarin.Forms.Image objImage;
..
..

..
if (objImage.Source is Xamarin.Forms.FileImageSource)
{
    Xamarin.Forms.FileImageSource objFileImageSource = (Xamarin.Forms.FileImageSource)objImage.Source;
    //
    // Access the file that was specified:-
    string strFileName = objFileImageSource.File;
}
like image 142
Pete Avatar answered Sep 24 '22 04:09

Pete