I needed my WPF controls size in pixels to retrieve the correct sized image from a web service. The answer hit me one night and it’s very simple:
Create a known size image (say 100 x 100 pixels), put it in a image control on some bit of WPF screen (Hidden not collapsed), ensure no stretching by putting Stretch=None and a non-stretch value for both Horizontal and Vertical alignment and make the calculation from the Image controls actual width and height.
This bit of xaml shows what I did:
<Grid> <Image x:Name="A100x100ImageControl" Source="/images/100x100.jpg" Stretch="None" HorizontalAlignment="Left" VerticalAlignment="Top" Visibility="Hidden"/> <Image x:Name="ImageControl" Stretch="Uniform"/> </Grid>
A calculation with look something like this:
(In my case I wrapped it up in a UserControl with SizeTarget, a bound property, being a specific interface with just width and height that can be implemented elsewhere.)
SizeTarget.PixelWidth = (int)Math.Truncate(ActualWidth * 100 / A100x100ImageControl.ActualWidth);