2

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

György Kőszeg
  • 17,093
  • 6
  • 37
  • 65
Amal
  • 576
  • 1
  • 6
  • 26
  • 2
    What are you using the measurement for? If the `Graphics` is from something that is displayed on the screen then obviously the size changes with the screen resolution. That's the point. If you want the size for a specific resolution you could create a `Bitmap` at that resolution and use its `Graphics` object to measure the string instead. – juharr Nov 20 '15 at 12:46
  • You could also have a look at https://msdn.microsoft.com/en-us/library/system.drawing.graphics.pageunit(v=vs.110).aspx i.e. the PageUnit property – TheLethalCoder Nov 20 '15 at 12:49
  • This is entirely normal, font sizes are expressed in *points*. One point is 1/72 inches. When you increase the DPI of the video adapter to 125% then the inch-to-pixel mapping ratio changes. From 96 pixels per inch to 120 pixels per inch. So you need more pixels to get the same point size. And thus get a result from MeasureString that is about 125% larger. When you go up to 150% and did not declare your app to be dpiAware then the dpi virtualization feature of Windows kicks in. It lies that the dpi is 96 and rescales your window itself. Noticeable, the text is fuzzy. – Hans Passant Nov 20 '15 at 13:38
  • @HansPassant, I think question is not clear. I have same issue with 2 png files loaded from file. both are 128x128 pixel, one image is 75dpi and other one is 150dpi. `MeasureString` returns 14.xx for one and 28.xx for other one using same font, later when drawing both these images on form both are same size, but text on one image is half size of the other one. [Do PNGs (or JPGs) have a DPI](https://stackoverflow.com/q/27323561/1041046) – AaA Aug 14 '18 at 07:44
  • It all depends on the program that displays the images. Many do *not* pay attention to the image dpi and simply scale the image to fit the window/screen. In .NET the Graphics.DrawImage() method does pay attention, PictureBox doesn't. – Hans Passant Aug 14 '18 at 08:15

1 Answers1

1

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);
Community
  • 1
  • 1
György Kőszeg
  • 17,093
  • 6
  • 37
  • 65