views:

1128

answers:

2

Hello. From what I've understood the relationship point to pixel will depend on the screen resolution. So how can I calcule it at run-time in c#?

Thanks

+3  A: 

It is all in Screen object

    int bpp = System.Windows.Forms.Screen.PrimaryScreen.BitsPerPixel;
    int wid = Screen.PrimaryScreen.WorkingArea.Width;
    int ht = Screen.PrimaryScreen.WorkingArea.Height;

On my machine it gives:

bpp=32
width=1280
height=740
TheVillageIdiot
When working with the Screen object always keep in mind that some users are working with multiple monitors. Somewhere there is a function which returns the screen on which you application is visible at the moment.
Alexander
Sure @Alexander. Thanks for pointing it.
TheVillageIdiot
+2  A: 

If you're trying to get the DPI of the screen it's a bit trickier. You'll have to create a real Graphics object and query that.

For example, in the Load event of your main form:

using( Graphics g = CreateGraphics() )
{
    _dpiX = g.DpiX;
    _dpiY = g.DpiY; // In practice usually == dpiX
    _points = _dpiX / 72.0f; // There are 72 points per inch
}

Of course most monitors lie about the actual DPI and always return 72 or 96, or when large fonts are enabled 120. If you actually want to map a physical inch to a the screen you'll have to actually calibrate it with the user's help - having them pick a line that they measure to be 1 inch.

Paul Alexander