Why Image not loading on android - xamarin - c#

I put crypto.png image into drawable folder under Resources in Android Project.
After I right click on the image -> Build Action -> Embedded Resource.
In MainPage.xaml on the main project I try to load the image like that:
<Image x:Name="HeadImage"
WidthRequest="100"
HeightRequest="100"
MinimumHeightRequest="100"
MinimumWidthRequest="100"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"
Aspect="AspectFit"
Source="crypto.png"/>
But the image not load.
I try a second method like this in the c# code:
var HeadImage = new Image { Aspect = Aspect.AspectFit };
HeadImage.Source = ImageSource.FromFile("crypto.png");
And this method not worked again..

You need to let your image in drawable folder as Android Resource instead of Embedded Resource, and to use it, you need to:
HeadImage.Source = "crypto.png";
Also, before set the image, is a good practice to remove MinimumWidthRequest and MinimumHeightRequest. This is safe ONLY if you are sure the image have this minimum size. Otherwise, your image will not appear.
To know more about image in Xamarin, see here.
And to understand the difference between the ways of set an ImageSource see here.

Related

PNG image bad display xamarin

So I downloaded this start button image:image
I want to make a button with this as the image.I added the image to the project as an embedded resource , and then I did this:
this.StartBtn.ImageSource = ImageSource.FromResource("Fitness_App.Resources.Images.start.png");
while the xaml file looks like this:
<StackLayout>
<Label Text="The workout you have chosen consists of:"/>
<ListView x:Name="Presentation_ListView"/>
<Button x:Name="StartBtn"/>
</StackLayout>
this is the result: image
Any idea how I could make that start png fit nicely?Since it is a png i thought all the other stuff would dissapear.Thanks!

Location of images for Xamarin Forms projects

I've a Xamarin Forms project - with the 3 projects for UWP, iOS and Android.
Now I want to use images in my Xamarin Pages. As intended by Xamarin I want to use the same code for all three targets ... e.g.:
<Image Source="Assets/appIcon-256.png"/>
This works for iOS and UWP - both have a folder "Assets" where I placed the image. Ok, I've to maintain the image twice, but I've the same XAML code for both project.
But for Android, that doesn't seems to work. The image is not displayed in Android, if I put the png in the Assets folder of the Anroid project.
What can I do to use the same location for all images for all the three projects?
It's very easy.
IOS => You need put the images on "Resources" (note: no more routes) , for ANDROID => you need put the images on "Resources/drawable" .for UWP => put the images on root project UWP.
On Diferents platforms the images have diferents routes.
Then on the xaml files you can put <Image Source="imageName.jpg" />
Send feedback please.
UPDATE
You can put the images on UWP/Assets and then :
<ToolbarItem
Text="Search">
<ToolbarItem.Icon>
<OnPlatform x:TypeArguments="FileImageSource">
<On Platform="Android, iOS" Value="movies-search.png"/>
<On Platform="UWP" Value="Assets/movies-search.png"/>
</OnPlatform>
</ToolbarItem.Icon>
</ToolbarItem>
Why the path starting with "Assets/"? For Android Place your image to MyProject.Android/Resources/drawable. For iOS myProject.iOS/Resources/
and in xaml
<Image Source="appIcon-256.png"/>
It will work.
In addition for iOS if you have same picture with different sizes, you should rename them like "appIcon-256.png" "appIcon-256#2x.png" "appIcon-256#3x.png"
Replace your dashes with underscores. Example, ic_launcher_calendar.png
Try also to remove the digits at the end of the name.
Lastly, you can check out the naming conventions of drawable. http://petrnohejl.github.io/Android-Cheatsheet-For-Graphic-Designers/
Let me know if it works.

WPF Image won't show in Class Library Project

I can't display an image on WPF window during runtime. However, it is shown during design mode. The image is located in Images folder and Build action is set to Content as well is Copy to output to Copy Always. The output Any suggestions?
===EDIT====
The output type of a project is : Class Libray, if I change it to Window Application the bellow are workign fine. However I still need to build it is Class library.
2nd try
<Image Source="pack://siteoforigin:,,,/Images/Logo2.png" Grid.Row="0"/>
3rd try
<Image Source="pack://application:,,,/Images/Logo2.png" Grid.Row="0"/>
Also,
If I put a full path to the image location it is working, apparently this is not ideal situation:
<Image Source="C:/.../Images/Logo2.png" Grid.Row="0"/>
Rebuild your entire solution and try again. It probably didn't do a build with images in it when you tried it again.
Also use your 3rd try again then build action to content, no need to make a copy always.
After several trials the answer was quite simple:
For Class Library Project set your images Build Action to Resource and get a reference in Xaml as:
<Image Source="/YOURNAMESPACE;component/Images/Logo.png"/>
It won't work with png, but convert to jpg and write:
<Image Source="/YOURNAMESPACE;component/Images/Logo.jpg"/>
That works for me :)

WPF Binding image source from Project Resources

Ok i have in my project Resources about 5 Images. What i want to do is to Bind an Image.Source from my Project Resources. Im C# code its pretty easy, i just do :
ImageHolder.Source = Propetries.Resources.Image1.png.
How can this be done in XAML?
Something like this :
<Image Source={??????}/>
Thanks in advance.
Visual studio will create Resources folder and put your image file into it when you add image to the resx file.
In order to use this image in binding you will need to change build action from None to Resource. After that you can bind as follows:
<Image Source="Resources/your_image_name.png"/>
You can not bind directly to Propetries.Resources.your_image_name because of you will need to convert System.Drawing.Bitmap to WPF BitmapSource. But you can bind to strings in the Resource.resx:
<TextBlock Text="{x:Static properties:Resources.YourStringResource}"></TextBlock>
Read here how to convert System.Darwing.Bitmap to the WPF bitmap: Load a WPF BitmapImage from a System.Drawing.Bitmap
And here about binding to the values in the resx file: Get values from *.resx files in XAML
Make sure your Build Action for image is marked as Resource and then you can simply do this in your XAML -
<Image Source="Properties/Resources/a.png"/>
Assuming Propetries/Resources is folder structure in your project where your image is present.

Images not showing in a Windows Store Application

I am building a Windows Store (WinRT) application, and I have added in buttons to manipulate items in a list, I am using images for these buttons. I have followed numerous sets of information but can't get these images to show up, I have tried the following:
<Button>
<Image Source="/Images/add.png"/>
</Button>
<Button>
<Image Source="mx-app://Images/add.png"/>
</Button>
I have tried the above with the image set to copy local, and then with different build actions, the image just never shows when I run the application.
If you're including the images in the application package, they should be marked as Content (which they would by default when pulling adding them to the project).
Your first invocation should work, assuming you have a top level folder in your project called Images.
Your second invocation should be (it's just a more explicit version of the first):
<Button>
<Image Source="ms-appx:///Images/add.png"/>
</Button>
Try setting the build action on each image to Resource.
Then you should be able to write
<Button>
<Image Source="/Images/add.png"/>
</Button>
Right click on the image. Properties -> Build Action. Ensure That "Build Action" is Content

Categories

Resources