Get physical monitor size

Possible duplicate:
How to determine the true pixel size of my monitor in .NET?

How to get the size of a monitor I mean its physical size, like width and height and diagonal, for example, 17 inches or what

I do not need a 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(); } } } 

he gives an error

Could not find type name or namespace 'ManagementObjectSearcher'

and it only works for Vista, I need a much wider solution

Also i tried

 Screen.PrimaryScreen.Bounds.Height 

but it returns permission

+6
source share
2 answers

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

 [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; } 
+4
source

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

+3
source

Source: https://habr.com/ru/post/890638/


All Articles