2012-02-25 97 views
0

我在windows phone的是新的,图像的高度和宽度的问题7

现在这样只是想显示堆栈面板的图像的样本。

我想显示图像的实际高度和宽度。但实际的高度和宽度返回0.

实际上,图像的高度为360px,宽度为480px。

我在下面发布我的代码。请帮助。

谢谢。

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); 
    } 
    } 
} 

回答

1

他们是零,直到调用myImage.Source = bit;此前MYIMAGE后,只是没有按一个空的图像没有任何内容。

+0

我重排的代码为u说。但高度和宽度仍然返回0.只需检查上面的编辑代码。 – Arun 2012-02-25 05:48:53

+0

这个网站:http://www.i-programmer.info/programming/wpf-workings/520-bitmapimage-and-local-files-.html?start=1可能有一些与你所看到的相关的更多细节? – 2012-02-25 06:08:31

0

ActualHeight和ActualWidth是布局和渲染后的高度/宽度。当你得到它时,它还没有被绘制。

刚刚摆脱这一部分,它应该工作:

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