Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with PrimaryScreen.Size

Tags:

c#

winforms

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

like image 792
Chuck Savage Avatar asked Mar 07 '13 21:03

Chuck Savage


2 Answers

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

like image 150
sa_ddam213 Avatar answered Oct 05 '22 19:10

sa_ddam213


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.

like image 38
Chuck Savage Avatar answered Oct 05 '22 19:10

Chuck Savage