acquire monitor physical dimension [duplicate]
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);
}
开发者_JAVA技巧 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
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;
}
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.
精彩评论