Graphics dpi not correct

G

Guest

I am in the load event of a user control. I get its Graphics with
this.CreateGraphics()

The DpiX and DpiY are both always 96.0 This does not vary, regardless of the
actual resolution of the screen. I have tested the value in code that is
executed after the load event, and the value is the same.

The docs say that Dpix is "the horizontal resolution supported by this
Graphics". I don't know if this is supposed to mean the current resolution.
If not, it doesn't seem to be a very useful datum.

I have to draw accurately on the screen regardless of resolution. Any
solution would be helpful.

Thx
Marc
 
H

Herfried K. Wagner [MVP]

MarcG said:
I am in the load event of a user control. I get its Graphics with
this.CreateGraphics()

The DpiX and DpiY are both always 96.0 This does not vary, regardless of
the
actual resolution of the screen. I have tested the value in code that is
executed after the load event, and the value is the same.

That's correct. If you change your system settings to "large fonts", the
value will be 120 DPI. The number of DPI does not represent the size of the
canvas.
 
L

Linda Liu [MSFT]

Hi Marc,

I agree with Herfried that the DpiX and DpiY property of the Graphics class
is independent on the actual screen resolution, instead it depends on the
font size settings on your machine.

If you're using Windows XP, you may change the font size by right-clicking
on the desktop and choose Properties. In the Display Properties window,
switch to 'Settings' tab and click the 'Advanced' button. In the property
window, switch to 'General' tab and you could set the DPI setting in the
'Display' groupbox.

If you're using Windows Vista, you may do it by right-clicking on the
desktop and choose Personalize and click the 'Adjust font size(DPI)' on the
left side in the coming up window.

To determine the current screen resolution, a simple solution is to use the
Screen class. The following is a sample.

int width = Screen.PrimaryScreen.Bounds.Width;
int height = Screen.PrimaryScreen.Bounds.Height;

Hope this helps.
If you have any question, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Linda, Herfried,

First, thanks for chasing me on this. I didn't mark Herfried's post as
"answered the question" because I was hoping that someone had a solution to
the problem.

The documentation in Graphics.DpiX is highly misleading. It should state
that DpiX is a default assumption based on the selected font size.

No functions of screen size (pixels) and DpiX/DpiY will allow you to create
a box on the screen that is reliably 1.5x3.0in (for example) in size. I
understand that nothing will let you do this on a CRT since the display area
can be stretched. LCDs are different.

Given the kinds of communications that can go on now between CPUs and
monitors including, at the least, make and model, to say nothing about laptop
specs that can be derived during OS installation, it isn't impossible to
think that MS could actually do better at deriving this information.

Thx
Marc
 
L

Linda Liu [MSFT]

Hi Marc,

Sorry for my delayed reply.

The technical term dots per inch(DPI) is generally used in printing text
scenario. The font size is not relevant to the actual screen resolution,
e.g. 1024*768 or 800*600.

Say that we have created an application with a form whose size is 150*150
when the screen resolution is 800*600. Then we change the screen resolution
to 1024*768. When we run the application at this time, the size of the form
doesn't change, i.e. it is still 150*150, but the form appears to be
smaller on the screen because the 'logical' size of the screen has been
enlarged so that the proportion of the form to the logical size of the
screen becomes smaller.

To maintain the displayed size of a form on the screen regarless of the
actual screen resolution, I think we could change the size of the form
based on the actual screen resolution. The following is a sample.

private void Form1_Load(object sender, System.EventArgs e)
{
this.Height = (this.Height * Screen.PrimaryScreen.Bounds.Height)/600;
this.Width = (this.Width * Screen.PrimaryScreen.Bounds.Width)/800;
}

Hope this helps.
If my suggestion is not what you want, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support
 
G

Guest

Thanks Linda,

You are exactly correct, so long as the app runs on a display that is the
same physical size as the design screen.

However, if I design on a 21" screen and deploy to a 15" screen the dialog
will not be the same visual size, even if the screen bounds are the same.

I need the user to tell me the physical height and width of the display
space on the each machine where the app is deployed. Knowing this, and
getting the screen bounds at runtime will let me calculate the pixel height
and width in inches, or mm.

Once I have that information, I can set the size of my screen objects in
pixels and my app will meet its requirement - you can measure the size of an
object with a ruler and it will be the same regardless of screen size in
either inches or pixels.

Let's call it quits here. Thanks for your help.

Marc
 
L

Linda Liu [MSFT]

Hi Marc,

Thank you for your detailed feedback on how you succeed in solving your
problem.

I think it will benefit all of us.

If you have any other question in the future, please don't hesitate to
contact us. It's always our pleasure to be of assistance!

Have a nice weekend!

Sincerely,
Linda Liu
Microsoft Online Community Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top