TextRenderer: how to measure text as if it were on a machine with different dpi?

I have a C # WinForms application that consists of a server and a client. I am using the TextRenderer.MeasureText(string text, Font font) method to measure text.

At some point, I need to measure the text on the server side, as if it were on the client. I send Graphics.DpiX and Graphics.DpiY values ​​from client to server. Based on these values, how can I measure text on the server side? The key point is that the client and Dpi server may be different.

I think I can somehow create a Graphics object from the Dpi values ​​and use the overload of TextRenderer.MeasureText(IDeviceContext dc, string text, Font font) to measure my text. But how to create Graphics only from the values ​​of DpiX and DpiY?

+7
c # text dpi winforms measure
source share
1 answer

You can try this hack: apply the conversion to the font size that you use to measure: drawing with 12pt at 120dpi will take the same number of pixels as drawing from 12 * 120/96 = 15 at 96 dpi.

0
source share

All Articles