Arun
Arun

Reputation: 3456

Image height and width issue in windows phone 7

I am new in windows phone ,

Now am doing a sample just trying to show an image in a stack panel.

I want to show the image in its actual height and width. But the actual height and width returns 0.

Actually the image with height of 360px and width of 480 px.

I posted my code below. Pls help.

Thanks.

MaingPage.xaml

<Grid x:Name="LayoutRoot" Background="Transparent">
    <StackPanel Name="imagePanel" ></StackPanel>
</Grid>

MainPage.xaml.cs

namespace ImageResizing
{
public partial class MainPage : PhoneApplicationPage
{
    Image myImage;
    BitmapImage bit;

    // Constructor
    public MainPage()
    {
        InitializeComponent();

        myImage = new Image();
        Uri uri = new Uri("Penguins.jpg", UriKind.Relative);
        bit = new BitmapImage(uri);
        myImage.Source = bit;

        myImage.Width = myImage.ActualWidth; // Returns 0
        myImage.Height = myImage.ActualHeight; // Returns 0

        myImage.Width = bit.PixelWidth;  // Also tried this. It returns 0 too
        myImage.Height = bit.PixelHeight; // Also tried this. It returns 0 too


        myImage.Stretch = Stretch.Fill;
        imagePanel.Children.Add(myImage);
    }
  }
}

Upvotes: 0

Views: 617

Answers (2)

Paul Hoenecke
Paul Hoenecke

Reputation: 5060

ActualHeight and ActualWidth are the height/width after it has been layed out and rendered. When you are getting it, it has not been drawn yet.

Just get rid of this part and it should work:

    myImage.Width = myImage.ActualWidth; // Returns 0
    myImage.Height = myImage.ActualHeight; // Returns 0

    myImage.Width = bit.PixelWidth;  // Also tried this. It returns 0 too
    myImage.Height = bit.PixelHeight; // Also tried this. It returns 0 too

Upvotes: 0

John Gardner
John Gardner

Reputation: 25106

They are zero until AFTER that call to myImage.Source = bit; Prior to that myImage is just an empty image that doesn't have any content.

Upvotes: 1

Related Questions