Easy. You would easily find a solution if you thought just a bit more.
public partial class FormMain {
void Print() {
using (PrintDocument pd = new PrintDocument()) {
if (PrinterSettings != null)
PrintDialog.PrinterSettings = PrinterSettings;
pd.PrintPage += (sender, eventArgs) => { this.Calculate(eventArgs.Graphics); };
DialogResult result = PrintDialog.ShowDialog();
if (result != DialogResult.OK) return;
PrinterSettings = PrintDialog.PrinterSettings;
PrintDialog.Document = pd;
pd.PrinterSettings = PrinterSettings;
pd.Print();
}
}
PrintDialog PrintDialog = new PrintDialog();
PrinterSettings PrinterSettings;
}
Your
Calculate(Graphics g)
is actually a good idea, but you probably underestimate it. You abstract some rendering from concrete instance of
System.Drawing.Graphics
. This way, you can have only one graphics rendering method and call it from some controls
OnPaint
but also use it for other targets of rendering: "Print", "Print to…", "Export to bitmap", "Export to…" (some vector graphics) and the like — quite universal. Are you getting the idea?
—SA