How to Determine the True Pixel Size of My Monitor in .Net

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

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.)

Is there a way to determine the physical size of the monitor?

I am thinking about two ways that might work under special conditions:

  • Try to get the name of the monitor hardware. On my dual-monitor system, I use one SyncMaster 205BW and one SyncMaster 173T. These are 20″ and 17″ monitors. You see what I mean? One problem, however, is that I am not sure if you programmatically can obtain these strings. Windows appears to only obtain SyncMaster: screenres.png.

  • You can use GetDeviceCaps(GetDC(GetDesktopWindow), VERTSIZE) to obtain "Height, in millimeters, of the physical screen." and similarly with HORZSIZE and the "width". But this will only work if you have calibrated your display, I believe. At least on my system, the values are much larger than the actual height and width...

  • I have no idea about your context, but if your app really does need the physical size of the end-user's output device, why not ask him? You could easily ask for the monitor's size during setup (e.g. using the excellent Inno Setup), and store the value in the registry. Then it is trivial to write a procedure GetPhysicalMonitorSize that simply reads the value from the registry.

How can I determine the screen width?

I cant tell you if this solves the problem but you can try it.

If you are getting different resolution on .NET's

System.Windows.SystemParameters.PrimaryScreenWidth

and

System.Windows.SystemParameters.PrimaryScreenHeight

than your current resolution settings, you might wanna check out your viewing settings.

To do so, go to your desktop, right click, and select "Screen resolution".

Then, click on "Make text and other items larger or smaller"

After this, click on the Smaller - 100% radio button.

it solves the problem for me once but again , cant say if it will solves your problem. hope it helps

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

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.)

Asp.Net Get Screen Width

You could read it with javascript and submit the results to the server.

A server-side-only solution can not exist, since html does not submit such data automatically in requests.

How to programmatically measure string pixel width in ASP.NET?

Like Tom Gullen is saying. You can just create a bitmap and messure the string. I have this code I use to find the width/length in pixel. Just change the font and size.

// Bitmap class namespace:
using System.Drawing;

...

private float GetWidthOfString(string str)
{
Bitmap objBitmap = default(Bitmap);
Graphics objGraphics = default(Graphics);

objBitmap = new Bitmap(500, 200);
objGraphics = Graphics.FromImage(objBitmap);

SizeF stringSize = objGraphics.MeasureString(str, new Font("Arial", 12));

objBitmap.Dispose();
objGraphics.Dispose();
return stringSize.Width;
}

Just wanted to show an example.

How can I get the active screen dimensions?

Screen.FromControl, Screen.FromPoint and Screen.FromRectangle should help you with this. For example in WinForms it would be:

class MyForm : Form
{
public Rectangle GetScreen()
{
return Screen.FromControl(this).Bounds;
}
}

I don't know of an equivalent call for WPF. Therefore, you need to do something like this extension method.

static class ExtensionsForWPF
{
public static System.Windows.Forms.Screen GetScreen(this Window window)
{
return System.Windows.Forms.Screen.FromHandle(new WindowInteropHelper(window).Handle);
}
}

ASP.NET c# get screen width in pixels

You need to populate a hidden field with Javascript (see this question for how to do that), and then send that field back to the server so that it's avaialble within ASP.NET.

ASP.NET is incapable of reading detailed browser information like that directly (you're talking about sending some very specific information from the client's browser to the ASP.NET server where your application is hosted, and there are a lot of problems involved with doing something like that).

See this similar, but less detailed, question:

Asp.Net Get Screen Width



Related Topics



Leave a reply



Submit