2012-02-25 96 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