Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

windows

I want to display an image at 'true size' in my application. For that I need to know the pixel size of the display.

I know windows display resolution is nominally 96dpi, but for my purposes I want a better guess. I understand this information may not always be available or accurate (e.g. older CRT displays), but I imagine with the prevelance of LCD displays that this should be possible!

Is there a way to get the pixel size of my display?

Is there a way to determine if the pixel size is accurate?

.NET API's preferred (I couldn't find them), but Win32 is OK too, I'm happy to P/Invoke.

like image 204
Peter Tate Avatar asked Jan 07 '09 21:01

Peter Tate


People also ask

How do you determine pixel size?

The pixel dimensions may be determined by multiplying both the width and the height by the dpi. A digital camera will also have pixel dimensions, expressed as the number of pixels horizontally and vertically that define its resolution (e.g., 2,048 by 3,072).

How many pixels is a 1920x1080 monitor?

In the case of a monitor with an industry-standard Full HD 1080p resolution, this display has a resolution of 1920 x 1080. This means that the screen will have a width of 1,920 pixels while the height of the screen will be 1,080 pixels. This results in a grand total of 2,073,600 pixels on-screen.

What resolution is 2560x1440?

1440p is also called QHD (quad high definition) or WQHD (wide quad high definition) and is a display resolution that measures 2560 x 1440 pixels. This resolution is also commonly referred to as 2K (opens in new tab).


2 Answers

For the display size you'll want Screen.PrimaryScreen.Bounds.Size (or Screen.GetBounds(myform)).

If you want the DPI, use the DpiX and DpiY properties of Graphics:

PointF dpi = PointF.Empty;
using(Graphics g = this.CreateGraphics()){
    dpi.X = g.DpiX;
    dpi.Y = g.DpiY;
}

Oh, wait! You wanted actual, hold a ruler up to the monitor and measure, size?! No. Not possible using any OS services. The OS doesn't know the actual dimensions of the monitor, or how the user has it calibrated. Some of this information is theoretically detectable, but it's not deterministic enough for the OS to use it reliably, so it doesn't.

As a work around, you can try a couple of things.

  • You can try to query the display string of the installed monitor device (I'm not sure how to do that) and see if you can parse out a sensible size out of that. For example, the monitor might be a "ValueBin E17p", and you might deduce that it's a 17" monitor from that. Of course, this display string is likely to be "Plug and Play Monitor". This scheme is pretty sketchy at best.
  • You could ask the user what size monitor they have. Maybe they'll know.

Once you know (or think you know) the monitor's diagonal size, you need to find its physical aspect ratio. Again, a couple of things:

  • Assume the current pixel aspect ratio matches the monitor's physical aspect ratio. This assumes that (A) the user has chosen a resolution that is ideal for their monitor, and that (B) the monitor has square pixels. I don't know of a current consumer-oriented computer monitor that doesn't have square pixels, but older ones did and newer ones might.
  • Ask the user. Maybe they'll know.

Once you know (or think you know) what the monitor's diagonal size and physical aspect ratio are, then you you can calculate it's physical width and height. A2 + B2 = C2, so a few calculations will give it to you good:

If you found out that it's a 17" monitor, and its current resolution is 1280 x 1024:
12802 + 10242 = 2686976
Sqrt(2686976) = 1639.1998047828092637409837247032
17" * 1280 / 1639.2 = 13.274768179599804782820888238165"
17" * 1024 / 1639.2 = 10.619814543679843826256710590532"

This puts the physical width at 13.27" and the physical height at 10.62". This makes the pixels 13.27" / 1280 = 10.62" / 1024 = 0.01037" or about 0.263 mm.

Of course, all of this is invalid if the user doesn't have a suitable resolution, the monitor has wacky non-square pixels, or it's an older analog monitor and the controls aren't adjusted properly for the display to fill the entire physical screen. Or worse, it could be a projector.

In the end, you may be best off performing a calibration step where you have the user actually hold a ruler up to the screen, and measure the size of something for you. You could:

  • Have the user click the mouse on any two points an inch (or a centimeter) apart.
  • Draw a box on the screen and have the user press the up and down arrows to adjust its height, and the left and right arrows to adjust its width, until the box is exactly one inch (or centimeter) square according to their ruler.
  • Draw a box on the screen and have the user tell you how many inches/centimeters it is in each dimension.

No matter what you do, don't expect your results to be 100% accurate. There are way too many factors at play for you (or the user) to get this exactly correct, every time.

Be aware that 96 dpi is usually pretty close to accurate. Modern pixels on non-projected screens all tend to be about 0.25 mm, give or take, so you usually end up with about 100 physical pixels per inch, give or take, if the monitor is set to its native resolution. (Of course, this is a huge generalization and does not apply to all monitors. Eee PCs, for example, have pixels about 0.19 mm in size, if I remember the specs correctly.)

like image 194
P Daddy Avatar answered Oct 23 '22 03:10

P Daddy


sorry, you've got to P/Invoke for this information.

Here's the link that I utilized for it a while ago: http://www.davidthielen.info/programming/2007/05/get_screen_dpi_.html

like image 40
Stephen Wrighton Avatar answered Oct 23 '22 02:10

Stephen Wrighton