WPF Screen Resolution in Pixels

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:

    <Image x:Name="A100x100ImageControl" 
    Source="/images/100x100.jpg" Stretch="None" 
        HorizontalAlignment="Left" VerticalAlignment="Top" 
    <Image x:Name="ImageControl" Stretch="Uniform"/>

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 / 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s