2011-06-15 80 views
6

可能重複:
How do I determine the true pixel size of my Monitor in .NET?獲得監測物理尺寸

如何讓顯示器大小我的意思是它的或物理尺寸如何寬度和高度,對角線例如17英寸什麼

我不需要分辨率,我試過

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

     } 
    } 
} 

它給錯誤

類型或命名空間名稱'ManagementObjectSearcher'找不到

,它適用於僅適用於Vista,我需要更廣泛的解決方案

我也試過

Screen.PrimaryScreen.Bounds.Height 

但它返回的分辨率爲

+0

GetDeviceCaps可能會有所幫助。請參閱http://msdn.microsoft.com/en-us/library/dd144877(v=vs.85).aspx – hsmiths 2011-06-15 20:21:37

+1

顯示器的像素和真實尺寸之間完全不同, – AMH 2011-06-15 20:22:20

回答

4

您可以使用GetDeviceCaps() WinAPI與HORZSIZEVERTSIZE參數。

[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

您可以通過使用SystemInformation.PrimaryMonitorSize.WidthSystemInformation.PrimaryMonitorSize.Height得到當前屏幕的屏幕分辨率。從Graphics對象:Graphics.DpiXGraphics.DpiY可以得到的每英寸像素數。其餘的只是一個簡單的等式(畢達哥拉斯)。我希望有幫助, 大衛。

+0

可以給我示例代碼 – AMH 2011-06-15 20:35:15

+0

double MonitorSize = Math.Sqrt(Math.Pow(SystemInformation.PrimaryMonitorSize.Width/Graphics.DpiX)+ Math.Pow(SystemInformation.PrimaryMonitorSize.Height/Graphics.DpiY)) – David 2011-06-15 20:42:30

+0

我認爲這應該工作,雖然我沒有測試它尚未 – David 2011-06-15 20:43:04