Mohanavel
Mohanavel

Reputation: 1781

Find Current monitor's Size (Height and Width)

I am using dual monitor and I want to find the Current(where my application is active) monitor's height and width (note : not resolution). How can i achieve this. My WPF application display screen size options based on current screen size.

Upvotes: 0

Views: 8620

Answers (4)

Grass the Dinosaur
Grass the Dinosaur

Reputation: 46

This answer has two parts - please don't judge. The new one I'm quite uncertain but will give you better results. The older one I'm sure works somehow.

New answer

I think you've missed an incredibly important answer in one of the questions you viewed. There was an answer using the Graphics.DpiX and Graphics.DpiY by dividing them to the pixel resolution's x and y values respectively. Read it right here! Don't forget to read through the comments as well.

Because the amount of pixels in an inch differs in different monitors, this dude found out that Graphics.DpiX is the the amount of pixels in the X axis required to make an inch. The same goes for Graphics.DpiY which obviously points the pixels per inch in the y axis. In order to find the inches from the resolution, you divide these two. You can read more on the usage here and here. The second link is in Visual Basic but, imagining that you have previous C# knowledge you'd be able to figure out the syntax.

Be warned - I haven't tried any of the following yet!

Start out by referencing System.Drawing.

Next, theoretically (I haven't tried this out before so please do correct me if you can) you'd need to make a new Graphics class.

Graphics DesktopGraphics = Graphics.FromHdc(GetDC(IntPtr.Zero));

And now, using our new Graphics class that references the monitor in question, we grab the DPI and the screen resolution and divide the screen resolution by the DPI to get how many inches the screen is.

SystemInformation.PrimaryMonitorSize.Width / Graphics.DpiX for width and SystemInformation.PrimaryMonitorSize.Height / Graphics.DpiY for height. You need to assign those two to their respective variables and voila! Width and height of the screen - in inches.

Again if I do do something wrong, please tell me. I'm still a novice programmer so if I do something wrong, don't bash me or flame me - can you please suggest the appropriate code if I do so.


Old answer

The person who wrote; try System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height it's working in my case. was, in fact, telling you the height, in pixels, however. What you need to do is to convert this integer to inches.

One inch is equal to 96 pixels (or 96.0000000000011 to be more exact). So, write the following code;

    //Height
    Int MonitorHeightinInches = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height / 96;
    //Width
    Int MonitorWidthinInches = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width / 96;

Now you may reuse the code as much as you want, in your application. This works for me.

Upvotes: 2

TylerHaigh
TylerHaigh

Reputation: 298

If you're feeling adventurous, have a look in the registry keys. Specifically HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\DISPLAY and loop through the displays (or better still just the active display) and use the EDID information to get the screens width and height (and by Pythagoras theorem, the diagonal length) of the monitor

Upvotes: 2

ZombieSheep
ZombieSheep

Reputation: 29963

You will find it very difficult to get a monitor's physical dimensions. You might be able to get the resolution and DPI and work it out from there, but that won't necessarily give you the correct answer. Imagine a projector - you may know it's resolution and DPI but the physical size of the displayed image will depend on the distance to the screen. It's not just projectors, though. The manufacturing processes per monitor may result in slightly off figures, for example.

I would be very tempted to remove any reference to physical sizes and make a flowing layout that resizes adequately according to the resolution and DPI capabilities of your display.

Upvotes: 1

Manish Parakhiya
Manish Parakhiya

Reputation: 3798

try

       System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height

it's working in my case.

Upvotes: 0

Related Questions