2012-09-19 67 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