16

I've been fine with Screen.PrimaryScreen.Bounds.Size for some time, but on my Windows7 computer attached to my big-screen TV it is giving me incorrect values.

I read elsewhere to try SystemInformation.PrimaryMonitorSize but that gives the same values.

When I right click the desktop to get Screen Resolution, it says 1920x1080. The above two are giving me 1280x720.

I've also tried the WPF versions:

var w = System.Windows.SystemParameters.PrimaryScreenWidth;
var h = System.Windows.SystemParameters.PrimaryScreenHeight;
MessageBox.Show(new Size((int)w, (int)h).ToString());

The display size has been changed via the (right click the desktop) Personalize > Desktop options to be 150% (since the screen is 60" and you sit kind of far away).

How to detect this so the value's returned from the above can be adjusted?

Note: I've discovered how to get around this with a right-click executable and adjust the compatability to disable DPI virtualization, but I still need a programatic solution so I don't have to have user's adjust this themselves: See - http://msdn.microsoft.com/en-us/library/dd464660(VS.85).aspx#dpi_virtualization

Chuck Savage
  • 11,775
  • 6
  • 49
  • 69
  • @Grant What do you mean? The screen resolution is `1920x1080`, there is no secondary display. The computer is not a laptop. – Chuck Savage Mar 07 '13 at 21:07
  • At 150%, you explicitly have to tell Windows that you are DPI aware on don't need to be lied to. – Hans Passant Mar 07 '13 at 23:49
  • 2
    possible duplicate of [Winforms high DPI blurry fonts](http://stackoverflow.com/questions/13228185/winforms-high-dpi-blurry-fonts) – Hans Passant Mar 07 '13 at 23:49
  • @HansPassant So there is no way to detect this from within the application? I've tried setting the manifest to test that, but I get a null error in a core lib. – Chuck Savage Mar 08 '13 at 00:11
  • @HansPassant I copied the manifest from the link over my manifest, and get the error "Value cannot be null.\r\nParameter name: activationContext" – Chuck Savage Mar 08 '13 at 00:18
  • You did something wrong, hard to guess. The linked duplicate also shows how to do it without a manifest. There's no point in "detecting it", your app is either dpi aware or it isn't. – Hans Passant Mar 08 '13 at 00:20
  • @HansPassant It must be, because I created a new WinForms project, did the exact same thing and it runs w/o the error. – Chuck Savage Mar 08 '13 at 00:27
  • I'm having the exact same problem in Windows 10. – Professor of programming Mar 08 '16 at 12:21

2 Answers2

6

It could be your Dpi setting in windows set above 100%

Try using this method, this will scale the resolution to the current system Dpi settings

Winforms:

private Size GetDpiSafeResolution()
{
    using (Graphics graphics = this.CreateGraphics())
    {
        return new Size((Screen.PrimaryScreen.Bounds.Width * (int)graphics.DpiX) / 96
          , (Screen.PrimaryScreen.Bounds.Height * (int)graphics.DpiY) / 96);
    }
}

WPF:

private Size GetDpiSafeResolution()
{
    PresentationSource _presentationSource = PresentationSource.FromVisual(Application.Current.MainWindow);
    Matrix matix = _presentationSource.CompositionTarget.TransformToDevice;
    return new System.Windows.Size(
        System.Windows.SystemParameters.PrimaryScreenWidth * matix.M22,
        System.Windows.SystemParameters.PrimaryScreenHeight * matix.M11);
}

Note: Make sure your MainWindow is loaded before running this code

Chuck Savage
  • 11,775
  • 6
  • 49
  • 69
sa_ddam213
  • 42,848
  • 7
  • 101
  • 110
4

I don't feel this is a duplicate question, but the answer is the same as on another thread: https://stackoverflow.com/a/13228495/353147 As the question isn't about blurry fonts but why Screen.PrimaryScreen.Bounds.Size returns faulty information. It could help others.

I did run into an error message, that mscorlib threw an null error. From this thread http://forums.asp.net/t/1653876.aspx/1 I was able to discover that unchecking "Enable ClickOnce security settings" fixed it. This seems like a hack, but it works.

Community
  • 1
  • 1
Chuck Savage
  • 11,775
  • 6
  • 49
  • 69