2011-06-15 11 views
6

Duplicar posibles:
How do I determine the true pixel size of my Monitor in .NET?adquieren dimensión física

¿Cómo obtener el tamaño del monitor me refiero a su tamaño físico cómo anchura y altura y en diagonal, por ejemplo, 17 pulgadas o lo

No necesito la resolución, lo intenté

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(); 

     } 
    } 
} 

que dan error

El tipo o espacio de nombres 'ManagementObjectSearcher' no se pudo encontrar

y funciona para Vista sólo, necesito mucho más amplia solución

También probé

Screen.PrimaryScreen.Bounds.Height 

pero devuelve la resolución

+0

GetDeviceCaps puede ayudar. ver http://msdn.microsoft.com/en-us/library/dd144877(v=vs.85).aspx – hsmiths

+1

hay una diferencia total entre el píxel y la dimensión verdadera del monitor, – AMH

Respuesta

4

Puede usar el GetDeviceCaps() WinAPI con HORZSIZE y VERTSIZE parámetros.

[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; 
} 
3

, usted puede obtener la resolución de pantalla de la pantalla actual utilizando SystemInformation.PrimaryMonitorSize.Width y SystemInformation.PrimaryMonitorSize.Height. La cantidad de píxeles por pulgada que puede obtener desde un objeto Graphics: Graphics.DpiX y Graphics.DpiY. El resto es solo una ecuación simple (Pitágoras). Espero que eso ayude, David.

+0

¿podría darme un código de muestra? – AMH

+0

doble MonitorSize = Math.Sqrt (Math.Pow (SystemInformation.PrimaryMonitorSize.Width/Graphics.DpiX) + Math.Pow (SystemInformation.PrimaryMonitorSize.Height/Graphics.DpiY)) – David

+0

Creo que debería funcionar, aunque no lo he hecho lo probé aún – David