Windows DPI setting affects Graphics.DrawString
Asked Answered
A

2

6

I have created a new Bitmap object and now want do draw some text to it using GDI+. So I call Graphics.DrawString(...).

The problem is that the size of the string depends on Windows 7's DPI settings. Is there any way to make my text drawing independent of the windows settings?

PS: The DPI settings seem to affect text only. A rect for example stys the same size when changing the DPI...

Anemophilous answered 29/5, 2012 at 13:35 Comment(0)
A
17

Just found the solution myself: The key is to create the font with the parameter GraphicsUnit.Pixel. That way drawing strings gets independent from the system's DPI settings.

Anemophilous answered 29/5, 2012 at 13:41 Comment(1)
You saved the dayBairam
S
4

You are correct in that the DPI affects only drawable items that are measured in device-independent units. Fonts are typically measured in Points, where 1 point = 1/72 of an inch. Therefore a 10pt font will be the same size in INCHES on each and every screen resolution and will take up more or less pixels depending on the screen resolution and pixel density.

Everything measured in pixels (such as lines, shapes etc) will not be affected by DPI, but the actual physical size will vary depending on screen resolution and pixel density. Changing your code to measure fonts in pixels will indeed ensure that the text is the same pixel size on all screen DPI settings, but if you were to print to a printer you'll find that the text size will vary depending on printer resolution.

Son answered 29/5, 2012 at 13:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.