Xamarin.Forms Images not showing - xamarin

I am developing a Cross-Platform app in Xamarin Visual Studio 2019 and having issues with image display.
I am trying to show an image with the following XAML code.
<StackLayout Grid.Row="0" Grid.Column="0" Spacing="0" >
<Image
HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand"
Source="logo"
WidthRequest="{OnPlatform iOS=300, Android=250}">
</Image>
</StackLayout>
Problem is image not displaying on either preview or deployed app.
I took references from multiple forums including help from StackOverflow but couldn't tackle the issue.
Here are the points and methods I worked on to resolve the issue.
The file named logo is an android resource folder and is a proper .png file.
Property for the file is set to AndroidResource.
As per answers posted here, Xamarin Images not showing, file name does not have a hyphen in it, and also tried other posted answers on the same page.
Image size 307X80 pixels so it's well within range for memory or size.
Android Drawable folder also contain default xamarin_logo.png image and that file works fine. But not newly added images. Is there anything I am missing here to add these image files to the solution?

Either the image extension or image path or the build action for the image seems to be the problem in your case. I tried your code and got that working fine for both png and jpg images.
<StackLayout Spacing="0">
<Image HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"
Source="github.png" WidthRequest="{OnPlatform iOS=300, Android=250}" />
<Image HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"
Source="githubb.jpg" WidthRequest="{OnPlatform iOS=300, Android=250}" />
</StackLayout>
Note: I have removed the grid.row and grid.column indexing as I am not using my StackLayout inside a grid.
Please make sure whether you have kept your images directly inside the drawable folder in the Resources in Android. If not, you may have to give the Source name as FolderName\ImageName.Extension.
Refer below link for more details:
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/images?tabs=windows#local-images
Also, please ensure whether the BuildAction of your image is AndroidResource and Custom Tool is MSBuild:UpdateGeneratedFiles. Please refer to the below image.
I got the below output for the code which I posted above.
I hope that helps.

Your XAML code is Correct but you have missed the image extension .png you must define your image Extension(format) Ex: (image.png or image.jpeg) That is the reason the image is not displayed.
<StackLayout Grid.Row="0" Grid.Column="0" Spacing="0" >
<Image
HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand"
Source="logo.png"
WidthRequest="{OnPlatform iOS=300, Android=250}">
</Image>
</StackLayout>

Related

Xamarin Forms XAML Image control has quit working

I have a working app that displays images. I started a new app and copied the code but this new app does not display images. Puzzled, I have tried many, many things to debug it. I eventually made a plain vanilla new project with only an Image control. As usual, I added the picture to the project using Add Existing Item. I confirmed that the image is there. It does not display the image. I noticed something different in Solution Explorer. Under the Solution, the first project has Dependencies listed under it. This is new. My previous apps have never had that. Now every new project I create has it. I also noticed that the Properties of the image was Build Action of None. I tried changing it to Content but that did not help. At this point, I am baffled and stuck. Did something change in Visual Studio? Here is the image test XAML.
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:ImageTest"
x:Class="ImageTest.MainPage">
<StackLayout>
<!-- Place new controls here -->
<Image Source="gr8oz.png" />
<Label Text="Welcome to Xamarin.Forms!"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage>
As I said, I have a working app that has been released. I copied this code into a new app and it does not work. In my investigation, I have found that the Image control does not work for me anymore.
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:OFamV2"
Style="{StaticResource MyContentPage}"
x:Class="OFamV2.MainPage"
Title="Main Menus">
<ScrollView>
<StackLayout>
<Button x:Name="TreeButton"
Text="Don and Iola Osborne"
VerticalOptions="Start"
Style="{StaticResource MyButtons}"
Clicked="TreeButton_Clicked" />
<Button x:Name="IndianNamesButton"
Text="Indian Names"
VerticalOptions="Start"
Style="{StaticResource MyButtons}"
Clicked="IndianNamesButton_Clicked" />
<Button x:Name="NextPageButton"
Text="Indian Potpourri"
VerticalOptions="Start"
Style="{StaticResource MyButtons}"
Clicked="NextPageButton_Clicked" />
<Button x:Name="OldPhotos"
Text="Old Photos"
VerticalOptions="Start"
Style="{StaticResource MyButtons}"
Clicked="OldPhotos_Clicked" />
<Button x:Name="ReunionButton"
Text="Family Reunion"
VerticalOptions="Start"
Style="{StaticResource MyButtons}"
Clicked="ReunionButton_Clicked"
/>
<StackLayout Orientation="Horizontal">
<Image
Source="Photos/gr8oz.png"
Style="{StaticResource MyImage}"
Margin="0,30,0,0"
Aspect="AspectFit"
HorizontalOptions="End"
/>
<Image
Source = "Photos/logo.bmp"
Style="{StaticResource MyImage}"
Margin="150,0,0,0"
HeightRequest="200"
WidthRequest="200"
HorizontalOptions="End"/>
</StackLayout>
</StackLayout>
</ScrollView>
</ContentPage>
I have searched and read everything that I can find (and yes, I have read the Microsoft doc about the Image class).
I am also curious why Dependencies suddenly appeared in the Shared Project folder. This is new too. How do I resolve them? See screenshot.
Dependencies under OFamV2.
I uninstalled Visual Studio, deleted the folder, reinstalled Visual Studio Community 2017 Version 15.8.8 and the problem is still there, even with a new project.
For Options, I select Cross Platform/ Mobile App/ Platform: Android and UWP/Code Sharing: .Net Standard.
Set image show you can see here , i think you have used that.
So reload your project,and rebuild it,you can try this.
More info:
Here is a similar disscussion about image show.
Suggest that better not copy code from other project about view,they may result some problems where you do not know.

Xamarin.Forms PCL cannot show image

I'm learning Xamarin.Forms PCL. I want to load image on page, i tried to load image with uri, that's OK. But i add image into project, it's don't work.
<StackLayout Margin="0,20,0,0">
<Image x:Name="img" Source="mario.jpg"/>
<Button x:Name="btnRotate" Clicked="btnRotate_Clicked" Text="Rotate" />
</StackLayout>
Any help is appreciated. Thanks!
You can not include dashes or spaces in image names for Android compatibility
Rename your image from piotr-chrobot-276746.jpg to something else, like piotrchrobot.jpg
https://developer.xamarin.com/guides/android/application_fundamentals/resources_in_android/part_1_-_android_resource_basics/
Don't forget to set the Build action to AndroidResource.

Is it possible to use Image Set in a Xamarin Forms

I am trying to add some Images as Image Sets in a Xamarin.Forms project. I followed this guide . But the icon is not displayed. Is it possible actually to use Image Sets in a Xamarin Forms Solution for the iOS project?
<Image VerticalOptions="Center"
HorizontalOptions="End"
Source="check_outline_500_active.png"
WidthRequest="40">
</Image>
Yes you can.
create the Asset catalog
add Image Set and name it "check_outline_500_active" (without .png)
add the images
done
my xaml
<Image VerticalOptions="Center"
HorizontalOptions="Center"
Source="check_outline_500_active.png">
</Image>
Result
Note
You might have to delete mtbs (on the mac) and bin/obj (on your project) or the build cache can fool you :)
Try to add images in resources folder under xamarin ios project and rename file like :
check_outline_500_active.png,
check_outline_500_active#2x.png,
check_outline_500_active#3x.png
but in code use : check_outline_500_active.png

Image Uri does not appear in XAML

I don't understand the problem : I have a link of an image but it does not visible :
<Image Source="http://www.bestofleboncoin.fr/img/adsok/thumbs/304409998.jpg" Margin="0,10,0,0" />
if I download this picture and save it in the solution it's ok !
<Image Source="/304409998.jpg" Margin="0,10,0,0" />
If I upload on my personal website, this image is visible too..
Can you download it using WebClient and then display it inside your control? Besides, you are supposed to just that for performance reasons.
It appears as if the site in question forbids direct linking.
I have a little solution but I don't know about performances ....
<phone:WebBrowser Name="Browser" Source="{Binding Links[1].Uri}" />

WinPhone Xaml Grid.Background relative path

I'm trying to set the background to a windows phone page in Xaml. I'm probably missing something really obvious but I can't seem to get this to show unless the page and background are in the same folder. What I want however is my page to be in a views folder and my background to be in a resources folder. This is what I have at the minute:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid.Background>
<ImageBrush ImageSource="\\Resources\MMCRBackground.png" Stretch="UniformToFill"/>
</Grid.Background>
</Grid>
There is no error saying that the path doesn't exist? Any ideas?
Thanks.
From memory I think you want
<ImageBrush ImageSource="/Resources/MMCRBackground.png" Stretch="UniformToFill"/>
Where the MMCRBackground.png has a build action of Content (not resource) in the properties from the Solution Exporer

Resources