I need to create a pen that draws with a width of 0.5 mm regardless of the scale of the graphics transformation matrix. That's what I'm doing:
Note: The sample code is C ++ / CLI, but responses using C # are welcome.
int dpi = g->DpiX;
float dotsPerHalfInch = dpi * 0.5;
array<PointF> ^points = gcnew array<PointF>{
PointF(0.0f, 0.0f), PointF(1.0f, 0.0f)
};
g->Transform->TransformPoints(points);
float dX = points[1].X - points[0].X;
float dY = points[1].Y - points[0].Y;
float distance = Math::Sqrt(dX * dX + dY * dY);
float penWidth = dotsPerHalfInch / distance;
Pen ^halfInchPen = gcnew Pen(Color::Black, penWidth);
It doesn't seem to work on the screen (96 dpi) ... gives me a pixel pen. On a PDF printer (600 dpi), it gives me a pen that is much thicker than half an inch.
What am I doing wrong? What is the correct way to create a non-scalable pen in GDI?
source
share