简体   繁体   中英

How to get the path of the image from stream in Xamarin.Forms

I'm covert the image as stream and set to the Xamarin.Forms.Image.Source, In button click, am trying to access the same image, but in code behind am getting the image as StreamImagesSource from that I can't able to access the get the stream.

Assembly _assembly = this.GetType().Assembly;
Stream stream = _assembly.GetManifestResourceStream("DataGrithWithImagePdfExport.Resources.Jk.png");
ImageSource source = ImageSource.FromStream(() => stream);
///Added the image to the ImageSource property in Model

Persons.Add(new Person() { Name = "John Smith", Photo = source });

Please helps me to overcome this issu. Thanks.

As per your answer in comment it seems like you haven't added your file as Embeded Resources(Jk.png).

If you want to use file from application manifest you need to set property as Embedded-Resource .

EDIT

If you place embedded images into folders within your project, the folder names are also separated by periods (.) in the resource ID.

in your question resource ID of DataGrithWithImagePdfExport.Resources.Jk.jpg mean :

  • DataGrithWithImagePdfExport - Namespace
  • Resources - Folder name
  • Jk.jpg - image File

For more information : https://docs.microsoft.com/en-us/xamarin/ios/app-fundamentals/images-icons/displaying-an-image?tabs=windows

Please check image below:

请看下面的图片

@Bala Malarkodi, Why are you accessing the stream ? You can simply take the file path, suppose if you are having sample.png under Images Folder then set your Property like this,

public class Person
{
   public string Name { get; set; }
   public string ImageURL { get; set; }
}

And prepare your model like this,

Persons.Add(new Person() 
 {
   Name = "John Smith",
   ImageURL = "Images/sample.png" 
 });

and Bind this ImageURL in your Page (xaml).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM