Get Monitor Size programmatically


Hello everyone,

today I want to show you how you can use C# to get the physical size of a monitor in inches. This can be helpful if you need to know the physical size of a monitor and you do not know where it is located or if you want to get a list of average monitor sizes in your company.

All you need to do to make this work is the below code snippet:

using System;
using System.Management;

namespace GetScreenSize
{
    class Program
    {
        static void Main(string[] args)
        {
            //Get monitor data
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"\root\wmi", @"SELECT * FROM WmiMonitorBasicDisplayParams");

            //Calculate and output size for each monitor
            foreach (ManagementObject managementObject in searcher.Get())
            {
                //Calculate monitor size
                double width = (byte)managementObject["MaxHorizontalImageSize"] / 2.54;
                double height = (byte)managementObject["MaxVerticalImageSize"] / 2.54;
                double diagonal = Math.Sqrt(width * width + height * height);

                //Output monitor size
                Console.WriteLine("Monitor Size: {0:F1}\"", diagonal);
            }

            Console.ReadLine();
        }
    }
}

If you have multiple monitors connected to your computer the above code will output the size of each monitor.

Important: You need to add a reference to System.Management to make the above code work.

Well, that´s it. Now you are able to get the physical size of your monitor without having to measure it.
I hope you liked my post and I hope to see you again next time 🙂

As always you can download my code from here.

Sources:

http://www.c-sharpcorner.com/forums/get-the-monitor-size

https://richardspowershellblog.wordpress.com/2011/06/15/rootwmimonitor-information/

http://stackoverflow.com/questions/4314630/managementobject-class-not-showing-up-in-system-management-namespace

Advertisements
Tagged ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: