[英]width and height of an Stretched Image
如果UniformToFill
拉伸了Image
,可以獲取圖像的Height
和Width
嗎?
我嘗試了Width
和Height
屬性,但它們始終是NaN
。
如果您想知道圖像控件的尺寸,可以這樣:
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;
同樣,這將為您提供調整大小后(但在均勻化之前)的圖像尺寸:
double actWidth = img.ActualWidth;
double actHeight = img.ActualHeight;
因此,這些變量之一( actWidth
或actHeight
)必須等於圖像控件尺寸,而另一個則要大於它。
請注意,如果您在Window_Loaded
事件中調用第二和第三代碼,則它們將Window_Loaded
,因為此時圖像尚未加載。 加載完所有內容后,您應該使用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.