Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows DPI setting affects Graphics.DrawString

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

like image 767
Boris Avatar asked May 29 '12 13:05

Boris


People also ask

What is DPI scaling Windows 10?

Dots per inch (DPI) is the physical measurement of the number of individual dots that can be placed in a line within the span of 1 inch. DPI setting controls the size of the text, apps and icons. A lower DPI setting will make them appear smaller and a higher setting will make them appear bigger.

What is DPI font?

A 12-point font is 16 pixels tall. 12 points = 12/72 logical inch = 1/6 logical inch = 96/6 pixels = 16 pixels. This scaling factor is described as 96 dots per inch (DPI). The term dots derives from printing, where physical dots of ink are put onto paper.

What is high DPI?

High DPI stands for High Dots Per Inch. It represents the pixel density; the higher the DPI, the higher the density of pixels. On 4K screens, the pixel density can be so high that displaying, as usual, would make all icons and controls extremely small.

How do I check my DPI on Windows 10?

Double-click Display icon (can also right-click on desktop and select Properties). Select Settings. Select Advanced. Under the General tab, find the DPI setting.


2 Answers

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.

like image 109
Boris Avatar answered Nov 06 '22 16:11

Boris


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.

like image 43
pdriegen Avatar answered Nov 06 '22 17:11

pdriegen