How to display the image when the build action of the image file is the content

TS44 81 Reputation points
2020-11-25T23:16:56.897+00:00

Currently, I am displaying an image using the build action of an image file as an embedded resource, but I do not know how to display the image when the build action is changed to content.

Someone please show me a sample

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,325 questions
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,951 Reputation points
    2020-11-26T03:22:33.777+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    1.To display the images in Xamarin.Forms, try placing the image file in the resource folder of the each platform project. Or if you place the files in the shared project, please set the Build Action to EmbeddedResource.

    To load images in Xamarin.Forms, you could check this document:
    https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/images?tabs=windows#embedded-images

    2.For the file which is set Build Action to Content, the VS provides to Application.GetContentStream method to access the file. But it is only supported in ASP.NET, it doesn't work in the .Net Standard referenced by Xamarin.Forms.

    Tutorial:
    https://learn.microsoft.com/en-us/visualstudio/ide/build-actions?view=vs-2019#build-action-values

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. mc 4,111 Reputation points
    2020-11-26T01:21:34.577+00:00

    please check this video
    https://channel9.msdn.com/Shows/XamarinShow/XamarinForms-101-Using-Embedded-Images

    for example:

    Image embeddedImage = new Image
    {
        Source = ImageSource.FromResource("WorkingWithImages.beach.jpg", typeof(MyClass).GetTypeInfo().Assembly)
    };
    
    0 comments No comments