Amal
Amal

Reputation: 586

MeasureString() gives different size for different screen resolutions

We are using Measurestring() to calculate size based on the length of text. For different screen resolution, MeasureString() gives different size.

Graphics g;  
Size size = g.MeasureString(GetItemText(this.Items[n]), this.Font).ToSize();  
width=size.width;       

For Screen resolution 125%, size.width=76 and For Screen resolution 100% and 150%, size.width=61.

How can i get same width in this code, please suggest me some ideas to measure size using measurestring().

Waiting for suggestions........

Upvotes: 2

Views: 2071

Answers (1)

György Kőszeg
György Kőszeg

Reputation: 18013

It's because the 125% behaves differently by default. For example, in Windows 7, if you change the DPI setting, because of the Windows XP style mode, the applications will aware of the current DPI setting. However, if you set 150%, this checkbox is not set by default, so the applications will work in DPI unaware mode, which means, that the MeasureString will return the same result as in case of 96 DPI, and the resizing will be performed automatically by Windows.

Windows DPI Setting

Normally you can ignore the result, because the sizes will be upscaled in your application anyway. If you still want to obtain the actual DPI value of Windows, see my answer here: https://stackoverflow.com/a/33412669/5114784

And then you can upscale your drawing like this (but as I said, normally this is not needed):

// See GetSystemDpi in the referenced post
float zoomFactor = (float)GetSystemDpi() / 96f;
size.Width = (int)(size.Width * zoomFactor);
size.Height = (int)(size.Height * zoomFactor);

Upvotes: 1

Related Questions