2012-09-19 69 views
2

我能得到一個ImageHeightWidth,如果它已經通過UniformToFill拉伸寬度和高度

我試過WidthHeight屬性,但他們總是NaN

回答

4

如果你想知道圖像控件的尺寸,這是這樣的:

double theHeight = this.ActualHeight - img.Margin.Top - img.Margin.Bottom; 
double theWidth = this.ActualWidth - img.Margin.Left - img.Margin.Right; 

img是在代碼中的圖像控制名稱的上方,然後在下面的代碼)


如果你想知道圖像的實際尺寸(拉伸前)你可以試試這個:

BitmapSource SourceData = (BitmapSource)img.Source; 
double imgWidth = SourceData.PixelWidth; 
double imgHeight = SourceData.PixelHeight; 

(我發現這個here


而且這將讓您圖像的尺寸調整後(但均一化前):

double actWidth = img.ActualWidth; 
double actHeight = img.ActualHeight; 

因此,這些變量之一(actWidthactHeight)必須等於圖像控制尺寸,而另一個將高於它。


請注意,第二和第三代碼不工作,如果你給他們打電話的Window_Loaded事件,因爲圖像是在那個時刻未加載。您應該在加載所有內容後使用它。

+0

爲什麼你問兩個問題,然後回答他們兩個?這些問題是否真的有必要? – Default