views:

217

answers:

5

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.

+1  A: 

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

Rob Fonseca-Ensor
I've tested it, the result is the same with first post of mine !
Mohammad
ok that's weird. What happens if you change you screen's resolution from the control panel?
Rob Fonseca-Ensor
I changed the resolution to 1680*1050, the result: height = 1002.0 ; width=1680.0. BTW:The DPI is 125%
Mohammad
+1  A: 

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.

Akash Kava
+5  A: 

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.

Hans Passant
A: 

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

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

deepu
+2  A: 

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.

JeffFerguson
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.
macias