4

How can I get screen resolution in console app (if it's possible)?

While in Forms I can use:

int height = Screen.PrimaryScreen.Bounds.Height;
int width = Screen.PrimaryScreen.Bounds.Width;

but I'm looking specifically console way.


So the way to solve my problem was proposed by Marc-Antoine Jutras. I need int values so I went like this:

    int height = Convert.ToInt32(SystemParameters.PrimaryScreenHeight);
    int width = Convert.ToInt32(SystemParameters.PrimaryScreenWidth);
  • 4
    There's no specific way to do this for a console application, as the console is just a window after all; you can just reference `System.Windows.Forms` and use the same code as before. – Clint Dec 21 '17 at 18:23
  • 1
    @Clint no. it's possible – Sergey.quixoticaxis.Ivanov Dec 21 '17 at 18:29
  • 1
    You can use [GDI](https://stackoverflow.com/a/43656496/6583956). Just checked it - works like a charm. To use the snippet from the answer add `include System.Drawing; using System.Runtime.InteropServices;` – Sergey.quixoticaxis.Ivanov Dec 21 '17 at 18:29
  • 2
    @Sergey.quixoticaxis.Ivanov well yeah. But there’s many ways to skin a cat. This isn’t a “console specific way” of doing it. Of course you could p/invoke to get it, but why bother when adding a reference gets you there? – Clint Dec 21 '17 at 18:30
  • 1
    @Clint because PresentationFoundation is heavy as hell and Forms are also heavy and both bring a pile of dependecies. – Sergey.quixoticaxis.Ivanov Dec 21 '17 at 18:31
  • 1
    Old-school way as well if you want to play around: https://stackoverflow.com/questions/6415222/get-a-windows-bounds-by-its-handle – Aaron Dec 21 '17 at 18:32
  • 1
    System.Windows.Forms isn’t presentation foundation... but I get your point, though the dependencies and everything don’t really impact much, as the Jit’er will take care of ensuring you’re only using what’s necessary. – Clint Dec 21 '17 at 18:32
  • 1
    @Clint I've spoken of both (PF + Forms, I didn't say PF == Forms). It depends, if the code works on crappy PCs with HDD and Console app is meant to be a lightweight utility, loading PF can take as long as all other work. I'm not saying that using it is a no go though =) – Sergey.quixoticaxis.Ivanov Dec 21 '17 at 18:34

3 Answers3

6
ManagementObjectSearcher mydisplayResolution = new ManagementObjectSearcher("SELECT CurrentHorizontalResolution, CurrentVerticalResolution FROM Win32_VideoController");
            foreach (ManagementObject record in mydisplayResolution.Get())
            {
                Console.WriteLine("-----------------------Current Resolution---------------------------------");
                Console.WriteLine("CurrentHorizontalResolution  -  " + record["CurrentHorizontalResolution"]);
                Console.WriteLine("CurrentVerticalResolution  -  " + record["CurrentVerticalResolution"]);

            }
Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61
1

You can use the System.Windows namespace, in the SystemParameters class you have the following properties:

PrimaryScreenWidth

PrimaryScreenHeight

I believe someone made a reference answering this question: Get and Set Screen Resolution

But you will have to add the PresentationFramework.dll to your console project.

using System.Windows;

namespace DispResolution
{
    class Program
    {
        static void Main(string[] args)
        {
            double height = SystemParameters.PrimaryScreenHeight;
            double Width = SystemParameters.PrimaryScreenWidth;
        }
    }
}
  • 2
    If another question has an answer that solves this one, please mark this question as a duplicate rather than posting an answer. Or post a comment instead. – Clint Dec 21 '17 at 18:33
  • 2
    it doesn't work because the DPI is set 125%, and the width and height become (1536, 864) instead of (1920, 1080) – sowen Mar 29 '18 at 16:01
0

WMI is an option;

int width;
int height;
var managementScope = new System.Management.ManagementScope();
managementScope.Connect();
var q = new System.Management.ObjectQuery("SELECT CurrentHorizontalResolution, CurrentVerticalResolution FROM Win32_VideoController");
var searcher = new System.Management.ManagementObjectSearcher(managementScope, q);
var records = searcher.Get();
foreach (var record in records)
{
    if (!int.TryParse(record.GetPropertyValue("CurrentHorizontalResolution").ToString(), out width))
    {
        throw new Exception("Throw some exception");
    }
    if (!int.TryParse(record.GetPropertyValue("CurrentVerticalResolution").ToString(), out height))
    {
        throw new Exception("Throw some exception");
    }
}
Output: 
Width:  1680
Height: 1050
lucky
  • 12,734
  • 4
  • 24
  • 46
  • I sort of worked for me in Windows 10. I have multiple monitors, but it only seems to return the resolution of one of them. – Dave Jun 11 '18 at 17:49