draw rectangle in millimeters doesn't show accurate width
I am drawing a rectangle in millimeter on a panel using the following code in c# by entering the width and length in mm at runtime. However the resultant rectangle drawn varies in size in different monitors. I want the rectangle to appear same size irrespective of the running the app in any monitor. Can any1 help me?. currently the width for 10mm measures 12mm and length for 10mm shows 11mm using a scale. I tested the app on different monitors, there again it shows different length. Is their 开发者_如何转开发anyway that I can show it to be of same width and length?
void panel1_Paint(object sender, PaintEventArgs e)
{
SolidBrush ygBrush = new SolidBrush(Color.YellowGreen);
g = panel1.CreateGraphics();
g.PageUnit = GraphicsUnit.Millimeter;
int w = Int32.Parse(textBox1.Text.ToString());
int h = Int32.Parse(textBox2.Text.ToString());
rct = new Rectangle(94, 27, w, h);
g.FillRectangle(ygBrush, rct);
}
Most displays aren't properly configured so that the computer knows the DPI (dots-per-inch). Physical units like millimeters only work if the computer knows how many pixels are in a millimeter, both horizontally and vertically.
Essentially, the method you're using is correct - but you're very unlikely in the real world to come across properly-configured machines.
If you needed to solve this problem in your software you could perhaps include a "configuration" option where the user would be responsible for setting the scale of the application.
精彩评论