Home > database >  unable to load image in xamarin forms
unable to load image in xamarin forms

Time:05-20

code is - in xamarin -

in code behind var image = new Image { Source = "//Assets//food.jpg" };

image is not loadind in actual xamarin form.

CodePudding user response:

from the docs

Android - Place images in the Resources/drawable directory with Build Action: AndroidResource. High- and low-DPI versions of an image can also be supplied (in appropriately named Resources subdirectories such as drawable-ldpi, drawable-hdpi, and drawable-xhdpi).

CodePudding user response:

From document Images in Xamarin.Forms,we know that there are several methods to display images in xamarin.

1.Local images

Image files can be added to each application project and referenced from Xamarin.Forms shared code. This method of distributing images is required when images are platform-specific, such as when using different resolutions on different platforms, or slightly different designs.

In code is:

 <Image Source="waterfront.jpg" />

Or

 var image = new Image { Source = "waterfront.jpg" };

Note:

We should put images into correct folder, in android ,we should put image to folder drawables.

To use the same image filename across all platforms the name must be valid on all platforms. Android drawables have naming restrictions – only lowercase letters, numbers, underscore, and period are allowed – and for cross-platform compatibility this must be followed on all the other platforms too. The example filename waterfront.png follows the rules, but examples of invalid filenames include "water front.png", "WaterFront.png", "water-front.png", and "wåterfront.png".

2.Embedded images

Embedded images are also shipped with an application (like local images) but instead of having a copy of the image in each application's file structure the image file is embedded in the assembly as a resource. This method of distributing images is recommended when identical images are used on each platform, and is particularly suited to creating components, as the image is bundled with the code.

To embed an image in a project, right-click to add new items and select the image/s you wish to add. By default the image will have Build Action: None; this needs to be set to Build Action: EmbeddedResource

If you place embedded images into folders within your project, the folder names are also separated by periods (.) in the resource ID. Moving the beach.jpg image into a folder called MyImages would result in a resource ID of WorkingWithImages.MyImages.beach.jpg.

Sample code:

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

For more, you can check: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/images?tabs=windows#local-images

And there is a whole sample included in above document, you can check it up here:https://docs.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/workingwithimages/ .

  • Related