Now Place:2uzhan.com » [RESOLVED] Common code to draw on screen and print WYSIWYG

[RESOLVED] Common code to draw on screen and print WYSIWYG

Visual Basic .NET @ April 11, 2010   Views:0


I've inherited a project in VB6.NET 2008. There is a Panel control where graphs are drawn along with titles, axes, legends etc. What is drawn on the panel is also printed WYSIWYG (on a print command, of course)

The problem I have is that the code for printing is entirely separate to the code for drawing. I want to merge the two so that common code is used. I'd like to work in the same units for both, which means setting either the pixels per inch for the Graphics object for the printing (so I can work in pixels) or doing the equivalent of the opposite so I can work in inches (printed inches, not screen inches).

I've been frustrated trying to find how to do either of these, or to know of a better way. Does anyone have any info or suggestions, please?




I now have a solution to my own question:

I have common code whose entry point takes 3 parameters: the Graphics object, pixels per unit in the X direction and pixels per unit in the Y direction.
To draw on the panel on the screen, I set the units of the Grapics object to pixels and calculate the pixels per unit parameters by dividing the resolutions by the number of inches of the output.
To print, I set the units of the graphics object to inches, and pass 1 for each of the pixels per unit parameters.
I multiply all X and Y values by the pixels per unit parameters.

It looks a bit clunky with having to do this multiplication for every parameter to every graphics primitive, but it's a solution.

Thank you for sharing your solution!

© 2018 2uzhan.com Contact