1

Been sitting on SO for couple hours and never found the correct answer. I'm writing a windows service in C#. This service should be able to use current screen dimensions to perform calculations on them. However, every possible way of obtaining that information returns dimensions of 1024x768 (seems to be just an arbitrary minimum of some sort). The actual screen resolution is 1920x1080. I've scoured the web and used three distinct ways. No luck so far. The ways mentioned are:

  • SystemInformation.MonitorSize from System.Windows.Forms
  • Screen.PrimaryScreen.Bounds from System.Windows.Forms
  • SystemParameters.PrimaryScreenHeight/Width from System.Windows.SystemParameters

The code snippet looks like the following:

    Rectangle rect = Screen.PrimaryScreen.Bounds;
    ServiceLog.WriteEntry("Screen dimensions from Screen.PrimaryScreen.Bounds: " + rect.Width + "X" + rect.Height);

    Size monitorSize = SystemInformation.PrimaryMonitorSize;
    int primaryScreenHeight = monitorSize.Height;
    int primaryScreenWidth = monitorSize.Width;
    ServiceLog.WriteEntry("Screen dimensions from Windows.Forms: " + primaryScreenWidth + "X" + primaryScreenHeight);

    int screenHeight = Convert.ToInt32(SystemParameters.PrimaryScreenHeight);
    int screenWidth = Convert.ToInt32(SystemParameters.PrimaryScreenWidth);
    ServiceLog.WriteEntry("Got screen dimensions: " + screenWidth + "X" + screenHeight);

I am at quite a loss here and seem to have no more ideas. Can anybody help?

fushigi
  • 91
  • 5
  • 1
    If you run that code in a normal user session, do you get the proper result? Is it only failing when run as a windows service? – yaakov Mar 02 '20 at 10:46
  • 2
    You should be very careful trying to interact with the desktop from a Windows Service, and the `System.Windows.Forms` namespace is almost certainly not going to be supported from within one either. – DavidG Mar 02 '20 at 10:48
  • 4
    A Service has no means to display UI and runs in session 0. Even if you could get the dimensions of "the" screen, what would you meaningfully do with that information? – Damien_The_Unbeliever Mar 02 '20 at 10:49
  • To test you need to use a different monitor. If you use the same monitor you will always get the same dimension. To test instead of getting the current monitor (SystemInformation.PrimaryMonitorSize) put in different sizes. – jdweng Mar 02 '20 at 10:51
  • 1
    @jdweng Um nope, OP isn't having a multiple monitor problem. – DavidG Mar 02 '20 at 10:51
  • 2
    seems more like having zero monitor problems – yaakov Mar 02 '20 at 10:52
  • @yaakov Yeah, it does determine the dimensions right, when started as a simple console app. – fushigi Mar 02 '20 at 10:52
  • @DavidG Nope, he doesn't. Although, it would be another topic entirely, anyway :) – fushigi Mar 02 '20 at 10:53
  • @Damien_The_Unbeliever I need that to determine what part of an image to use to detect image structure using imagemagick.net etc. etc. Wallpaper stuff. – fushigi Mar 02 '20 at 10:55

0 Answers0