Click here to Skip to main content
15,506,025 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create a text box with accurate size by 5 inch by 7 inch.

I created a simple Windows Form app and two labels and one textBox.
here is the Form1_Load event handler:

private void Form1_Load(object sender, EventArgs e)
 {
     using (Graphics g=this.CreateGraphics())
     {
         label1.Text = g.DpiX.ToString();
         label2.Text = g.DpiY.ToString();

         //width:5 inch
         //height:7 inch
         //textBox1.Size =new Size( (int)Math.Ceiling(5 * g.DpiX), (int)Math.Ceiling(7*g.DpiY));
         int width = (int)(5 * g.DpiX);
         int height = (int)(7 * g.DpiY);

         textBox1.Size = new Size(width, height);

     }
 }


but when I use a physical ruler to measure this textbox, I find out its width is more than 5 inch and height is much larger than 7 inch.

is there anything wrong with my understanding with this DpiX and DpiY values of Graphics object?

What I have tried:

I searched internet and did not find the good answer.
Posted
Updated 25-May-22 20:01pm

1 solution

DPI is not the same as resolution, and have nothing to do with the actual physical size of the monitor and everything to do with fonts.

The physical screen has two relevant sizes: Resolution, which is the actual number of pixels X and Y (which windows does know) and Size in cm or inches (which Windows may or may not know).
DPI is not related to either of those and the mapping to physical pixels is also affected by the Windows Zoom setting, which is a "global size changer".

As far as I know there is no reliable way to get the actual on-screen height or width in terms that the drawing system gat relate to without drawing a physical box on the screen and getting the user to actually measure it to give you an "adjustment factor" that works for the current monitor and the current monitor / adapter / Zoom settings - and even that may not work if the user drags the app to a second or third monitor!

Basically, you should assume that the user is comfortable with the text size he has picked and relate your app around "n lines of text" instead of trying to measure it in outside world terms as therein lies a world of pain.
 
Share this answer
 
Comments
Southmountain 26-May-22 9:36am    
thanks for the information! it is a little disappointing...
OriginalGriff 26-May-22 9:55am    
It makes sense though - if Windows can't identify the actual make and model of a monitor (and it can't for all) then it has absolutely no way way to tell how big a real pixel is. Let alone what happens when Windows is running in a VM with virtual hardware in a Linux window!
Southmountain 26-May-22 22:38pm    
it makes more sense to me now. thank you for your time!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900