2

How can I get the results of SystemInformation.VirtualScreen AND Screen.PrimaryScreen.Bounds to work the same both inside and outside of Visual Studio for a WinForms application?

private void ShowScreenSize()
{
    MessageBox.Show( SystemInformation.VirtualScreen.ToString() );
}

When I run the code above INSIDE Visual Studio it shows:{X=0,Y=0,Width=3840,Height=2160}

When I run the code above OUTSIDE Visual Studio it shows: {X=0,Y=0,Width=3072,Height=1728}

I also get the same value difference when using Screen.PrimaryScreen.Bounds
My OS is Windows 8.1 and my native monitor resolution is 3840x2160

Also, I notice UI scaling of the application is normal when run inside of Visual Studio, but is stretched slightly when outside. I'm guessing that Visual Studio prevents the built in Windows UI scaling as my system is configured for UI scaling one step larger than minimum. Might that be affecting the value of SystemInformation.VirtualScreen?

My final goal is to capture the full desktop using Graphics.CopyFromScreen(). I want to get the full rectangle that includes all monitors in the whole virtual desktop.

Irshad
  • 3,071
  • 5
  • 30
  • 51
Moon
  • 1,141
  • 2
  • 11
  • 25
  • 1
    Might be a duplicate of [this](http://stackoverflow.com/q/1317235/21567). – Christian.K Jan 03 '16 at 09:36
  • No, While I am using the value identified in that question, but the result is changing depending on whether I am inside or outside of VisualStudio. My question is how can I make it work the same outside as it does inside Visual Studio? – Moon Jan 03 '16 at 09:41
  • Maybe you should try using the `SystemParameters.VirtualScreenWidth` property instead - as it seems to be apt for WPF applications (if you have a WPF application). Otherwise just ignore this. ;-) – Christian.K Jan 03 '16 at 09:43
  • I am running a WinForms App, not a WPF app. I cannot see the SystemParameters object. – Moon Jan 03 '16 at 09:45
  • Yes, it would be defined in `PresentationCore.dll`. But never mind, I just check the implementations of both on `sourceof.net` and both are basically identical. Sorry about the confusion. – Christian.K Jan 03 '16 at 09:47
  • 1
    3840 / 3072 = 1.25. You must declare your app [dpiAware](http://stackoverflow.com/a/13228495/17034) if you don't want to be lied to. – Hans Passant Jan 03 '16 at 12:53
  • That fixed it - if you post that as answer I will mark it: Here is the code: `[System.Runtime.InteropServices.DllImport("user32.dll")] private static extern bool SetProcessDPIAware(); static void Main(string[] args) { if (Environment.OSVersion.Version.Major >= 6) SetProcessDPIAware();` – Moon Jan 03 '16 at 12:58

0 Answers0