7

Possible Duplicate:
How do I determine the true pixel size of my Monitor in .NET?

How to get the monitor size I mean its physical size how width and height and diagonal for example 17 inch or what

I don't need the resolution , I tried

using System.Management ; 

namespace testscreensize
{
    class Program
    {
        static void Main(string[] args)
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("\\root\\wmi", "SELECT * FROM WmiMonitorBasicDisplayParams");

            foreach (ManagementObject mo in searcher.Get())
            {
                double width = (byte)mo["MaxHorizontalImageSize"] / 2.54;
                double height = (byte)mo["MaxVerticalImageSize"] / 2.54;
                double diagonal = Math.Sqrt(width * width + height * height);
                Console.WriteLine("Width {0:F2}, Height {1:F2} and Diagonal {2:F2} inches", width, height, diagonal);
            }

            Console.ReadKey();

        }
    }
}

it give error

The type or namespace name 'ManagementObjectSearcher' could not be found

and it works for vista only , I need much wider solution

also I tried

Screen.PrimaryScreen.Bounds.Height

but it return the resolution

Community
  • 1
  • 1
AMH
  • 6,363
  • 27
  • 84
  • 135
  • GetDeviceCaps might help. see http://msdn.microsoft.com/en-us/library/dd144877(v=vs.85).aspx – hsmiths Jun 15 '11 at 20:21
  • 1
    there's a totally difference between the pixel and the true dimension of the monitor , – AMH Jun 15 '11 at 20:22

2 Answers2

5

You can use the GetDeviceCaps() WinAPI with HORZSIZE and VERTSIZE parameters.

[DllImport("gdi32.dll")]
static extern int GetDeviceCaps(IntPtr hdc, int nIndex);

private const int HORZSIZE = 4;
private const int VERTSIZE = 6;
private const double MM_TO_INCH_CONVERSION_FACTOR = 25.4;

void  Foo()
{
    var hDC = Graphics.FromHwnd(this.Handle).GetHdc();
    int horizontalSizeInMilliMeters = GetDeviceCaps(hDC, HORZSIZE);
    double horizontalSizeInInches = horizontalSizeInMilliMeters / MM_TO_INCH_CONVERSION_FACTOR;
    int vertivalSizeInMilliMeters = GetDeviceCaps(hDC, VERTSIZE);
    double verticalSizeInInches = vertivalSizeInMilliMeters / MM_TO_INCH_CONVERSION_FACTOR;
}
Bala R
  • 107,317
  • 23
  • 199
  • 210
  • This should be the chosen answer. It worked out perfect for me. I was even able to copy/paste. The only thing not explicitly stated here is that this requires the `System.Runtime.InteropServices` namespace. – Duu82 Feb 10 '19 at 12:39
4

You can get the screen resolution of the current screen by using SystemInformation.PrimaryMonitorSize.Width and SystemInformation.PrimaryMonitorSize.Height. The number of pixels per inch you can get from a Graphics object: Graphics.DpiX and Graphics.DpiY. The rest is just a simple equation (Pythagoras). I hope that helps, David.

David
  • 465
  • 2
  • 5
  • 16
  • double MonitorSize = Math.Sqrt(Math.Pow(SystemInformation.PrimaryMonitorSize.Width / Graphics.DpiX) + Math.Pow(SystemInformation.PrimaryMonitorSize.Height / Graphics.DpiY)) – David Jun 15 '11 at 20:42
  • I think that should work, although I haven't tested it yet – David Jun 15 '11 at 20:43
  • execuse me which Graphics do u mean by , forgive me but I am new to this – AMH Jun 15 '11 at 20:44
  • Im sorry, I didn't make that clear. A `Graphics` object is used to paint on, for example images or controls. It is contained in the `System.Drawing` namespace. You can also get a `Graphics` object from the desktop. This `Graphics` object should contain the DPI of the whole screen. You can get the `Graphics` object by using the following code: `DesktopGraphics = Graphics.FromHdc(GetDC(IntPtr.Zero));`. Then you can just calculate the monitor size (in inches) by using the code above (you need to use DesktopGraphics instead of Graphics, though). I hope this is the answer you were looking for. – David Jun 15 '11 at 20:53