Xamarin image dimensions are not correct

I want to display a 60 * 60px image in my Xamarin Forms based Android project using Visual Studio 2013 and latest Xamaring.

I followed these steps:

  • Place the png image in the Resources \ Drawable folder.
  • Set the "Build Action" of the image to AndroidResource
  • Created a Xamarin.Forms.Image object like this:
    protected static Image CreateHeaderLeftImage()
    {
        Image image = new Image();

        image.Source = Device.OnPlatform(null, ImageSource.FromFile("image.png"), null);

        image.WidthRequest = 60;
        image.HeightRequest = 60;

        image.VerticalOptions = LayoutOptions.Center;
        image.HorizontalOptions = LayoutOptions.Center;

        return image;
    }

      

  • In App.GetMainPage () I just set the content of the main page to the generated image
public static Page GetMainPage()
{
    ContentPage contentPage = new ContentPage();

    contentPage.Content = CreateHeaderLeftImage();

    return contentPage;
}

      

The image appears in the middle of the page at double the height and double the width! I took a screenshot and measured the image, it's 120 * 120 instead of 60 * 60!

I've used the code above from many Xamarin!

Why is the image enlarged?

+3


source to share


1 answer


Xamarin.Forms does not use pixels for sizing view elements.

http://forums.xamarin.com/discussion/18255/coordinate-units



So, you are not specifying that something should be 60px, but in case Android is going to be 60dpi.

+1


source







All Articles