I need to draw a line one inch long on any device given a Graphics reference to it. I need it to be an inch long regardless of what Transform is set to. Let’s assume that the scaling factor of the transform is given by scale in both horizontal and vertical directions.
Some C++/CLI code:
g->DrawLine(Pens::Black, 50.0f, 50.0f, 50.0f + oneInchEquivalent / scale, 50.0f);
Now that was not difficult at all! Now all we need to do is calculate oneInchEquivalent.
g->DpiX gives me a distance of what looks like one inch on screen but not on the printer. It seems that on printers, drawing a line of 100 units with g->PageUnit set to GraphicsUnit::Display will give me a line one inch long. But, I really need this to work regardless of the PageUnit setting. In fact, changing PageUnit will change the width of the pen!!
Edit: I have tentatively accepted the only answer here as it’s pretty close to what I am looking for.
The answer became rather long after a couple of edits, so here is the final result:
Setting the
PageUnitproperty of theGraphicsobject toGraphicsUnit.Pixeland taking in multiplying coordinates with the DpiX and DpiY values will render the expected result on both display and printer devices.You can have it paint on a Form (or some control):
…or send the output to a printer:
This does, however, rely on setting the
PageUnit.