12

I have Image control in my Window. The size of this Image control is set to "Auto".

<Image x:Name="videoImg" Stretch="Fill" Height="Auto" Width="Auto" />

When i try to get access, it returns 0. How to see real size of this control? It resizes with window.

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
Timur Mustafaev
  • 4,869
  • 9
  • 63
  • 109

3 Answers3

22

You can use .ActualHeight and .ActualWidth to get the rendered Height/Width of a control

Rachel
  • 130,264
  • 66
  • 304
  • 490
  • @TimurMustafaev How are you trying to get the values? Through code behind? Through a binding? And when are you trying to get the values? The control has to be rendered before `ActualHeigh` and `ActualWidth` will return a size. – Rachel Oct 26 '11 at 14:14
  • I call new form from my MainWindow. On this new form there are fiew controls and one of them is Image. After window loaded i try to get size like this: _imageControl.ActualWidth or _imageControl.ActualHeigh – Timur Mustafaev Oct 26 '11 at 14:45
  • @TimurMustafaev That should work providing the image is Visible (invisible objects do not have a render size). Test getting those values in a Button Click event to be sure the images has finished rendering. Another set of values you can look at is `.RenderSize.Height` and `.RenderSize.Width` – Rachel Oct 26 '11 at 15:00
3

The thing is, the Width and Height properties let you express the desired size, whereas what you want is the rendered size - which can be accessed (but not set) using the ActualWidth and ActualHeight properties.

It should be noted that these aren't static values either, that is, once set they are not necessarily going to be the same forever after, as it will be re-evaluated upon each rendering sequence...

Because [ActualHeight / ActualWidth] is a calculated value, you should be aware that there could be multiple or incremental reported changes to it as a result of various operations by the layout system. The layout system may be calculating required measure space for child elements, constraints by the parent element, and so on.

So, depending on your requirements, you might want to consider re-evaluating your data at appropriate points, perhaps when the containing control resizes, for instance.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
1

While some of WPF controls fill up all available space when laid out and rendered, the others don't. Specifically, the Image control is not of a kind that establishes its size on its own, i.e., in cases when you do not specify control's size explicitly with width/height attributes or the like.

But the Grid control fills up all available space when lacking size-defining attributes. The Page/Window template in Visual Studio has a Grid control as a child of a Page/Window root control, and when the user starts to put controls on a page in a graphical editor, the user-added controls first become children of this Grid control.

If you have used the VS template, and your Image control is a child of the said Grid control, name your Grid with an x:Name attribute, and you can use the Grid's ActualWidth/Height properties for your needs in a code-behind, because the image control grows up to its parent Grid size -- provided you do not specify its size explicitly or otherwise, i.e., setting the Image content.

By the way, the sizing behavior of built-in controls can be changed. You can modify a control and override corresponding dependency properties. See, for example, https://stackoverflow.com/a/6094993 .

V.V.T
  • 231
  • 1
  • 7