Screen Resolution Problem In WPF?
Asked Answered
D

8

27

I'm gonna detect the resolution with the following code in WPF :

double height = System.Windows.SystemParameters.PrimaryScreenHeight;
double width = System.Windows.SystemParameters.PrimaryScreenWidth;

Current resolution of my screen is 1920*1200, but height is 960.0 and width is 1536.0 !!!

What's wrong with it ?
Thanks in advance.

Dennet answered 10/2, 2010 at 10:52 Comment(0)
T
37

Keep in mind that all WPF locations and sizes are floating point with a unit of 1/96 inch. Not pixels. This makes your window designs resolution independent. Doing the math: height = 960 / 96 = 10 inches. With your video adapter set to 120 DPI (120/96 = 125%): 10 * 120 = 1200 pixels. Same for width: 1536 / 96 * 120 = 1920 pixels.

System.Windows.Forms works in units of pixels. You are getting less than 1050 for the height because it subtracts the height of the taskbar. But for WPF you always want to work with 1/96", never pixels.

Teilo answered 10/2, 2010 at 13:2 Comment(0)
T
27

For an even more robust implementation, you should calculate the DPI factors on your system and work with those factors. A normal DPI value is 96, but some monitors may have different values. Consider that your code may be running on a monitor that has a different DPI value than 96. Consider this code:

    private static void CalculateDpiFactors()
    {
        Window MainWindow = Application.Current.MainWindow;
        PresentationSource MainWindowPresentationSource = PresentationSource.FromVisual(MainWindow);
        Matrix m = MainWindowPresentationSource.CompositionTarget.TransformToDevice;
        thisDpiWidthFactor = m.M11;
        thisDpiHeightFactor = m.M22;
    }

You can then use those ratios to get the final values:

CalculateDpiFactors();
double ScreenHeight = SystemParameters.PrimaryScreenHeight * thisDpiHeightFactor;
double ScreenWidth = SystemParameters.PrimaryScreenWidth * thisDpiWidthFactor;

The values of ScreenHeight and ScreenWidth should then match what you see in your monitor's Properties window.

Taxiway answered 15/6, 2010 at 3:25 Comment(2)
It is nice, but you have a window in advance. How to calculate DPI not having window? It should be possible -- dpi is property of desktop, not just window.Wolbrom
There seems to be some code related to this here: mesta-automation.com/tecniques-scaling-wpf-applicationMeanie
S
2

Please call this after your windows is loaded.

 public static class Ext
{
    public static Size GetNativePrimaryScreenSize(this Window window)
    {
        PresentationSource mainWindowPresentationSource = PresentationSource.FromVisual(window);
        Matrix m = mainWindowPresentationSource.CompositionTarget.TransformToDevice;
        var dpiWidthFactor = m.M11;
        var dpiHeightFactor = m.M22;
        double screenHeight = SystemParameters.PrimaryScreenHeight * dpiHeightFactor;
        double screenWidth = SystemParameters.PrimaryScreenWidth * dpiWidthFactor;

        return new Size(screenWidth, screenHeight);
    }
}
Subvention answered 16/2, 2016 at 13:48 Comment(0)
L
1

Try SystemParameters.FullPrimaryScreenWidth and FullPrimaryScreenHeight, I believe PrimaryScreenWidth and Height returns size of available client window after removing the taskbar and other deskbands on your screen.

Limnetic answered 10/2, 2010 at 12:30 Comment(0)
A
0

you could use this instead: http://msdn.microsoft.com/en-us/library/system.windows.forms.screen.primaryscreen.aspx

Adjuvant answered 10/2, 2010 at 11:2 Comment(3)
I've tested it, the result is the same with first post of mine !Dennet
ok that's weird. What happens if you change you screen's resolution from the control panel?Adjuvant
I changed the resolution to 1680*1050, the result: height = 1002.0 ; width=1680.0. BTW:The DPI is 125%Dennet
S
0

If you check "Use Windows XP style DPI scaling", then the returned value of "Screen.PrimaryScreen.Bounds" is correct. If not, the returned value is proportionally shrunk by the DPI value (which is the default).

To find the "Use Windows XP style DPI scaling" checkbox, expand the "To make text and on-screen items clearer in programs that aren't designed for high DPI" link in the following link: http://windows.microsoft.com/en-us/windows-vista/Make-the-text-on-your-screen-larger-or-smaller

Succoth answered 3/5, 2012 at 15:28 Comment(1)
FYI: That link now automatically goes to Windows 8.x explanation, not XP/Vista/Win7Jaconet
F
0

You can use that:

float dpiX, dpiY;
using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
{
     dpiX = graphics.DpiX;
     dpiY = graphics.DpiY;
}

double screenX = SystemParameters.PrimaryScreenWidth / 96 * dpiX;
double screenY = SystemParameters.PrimaryScreenHeight / 96 * dpiY;
Fruitarian answered 24/7, 2019 at 19:50 Comment(0)
P
-1

try these..i believe this could correct the error.....

System.Windows.Form1.Screen.PrimaryScreen.Bounds.Height; System.Windows.Form1.Screen.PrimaryScreen.Bounds.Widtht;

Philately answered 15/3, 2010 at 10:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.