Saving Print Images

B

Bob Morris

I wish to use a PrintPreviewDialog to print documents and at the same time
store images of those docs for audit purposes.

This code saves the images alright, but nothing happens when the print
button in the DialogBox is clicked. If I try getting the
PreviewPrintController reference after the dialog closes (line commented
out) I get an invalid cast exception.

Somewhere the dialog must be setting the document PrintController property
to a PreviewPrintController, but how do I get at it?

Best Regards

//Start here
private void button1_Click(object sender, System.EventArgs e)

{

PrintDocument pdoc = new PrintDocument();

PrintPreviewDialog pdlg = new PrintPreviewDialog();

pdlg.Document = pdoc;

PreviewPrintController ppc = new PreviewPrintController();

pdoc.PrintController = ppc;

pdoc.PrintPage+=new PrintPageEventHandler(pdoc_PrintPage);

// PreviewPrintController ppc =
(PreviewPrintController)pdlg.Document.PrintController;

pdlg.ShowDialog();

PreviewPageInfo[] ppi = ppc.GetPreviewPageInfo();

Image im = ppi[0].Image;

im.Save("PrintImage.bmp");


}

private void pdoc_PrintPage(object sender, PrintPageEventArgs e)

{


Graphics g = e.Graphics;

g.DrawString("Hello World",new Font("Arial",12),Brushes.Black,new
PointF(100,100));

}

// to here
 
B

Bryan H. Haber[MSFT]

There are probably a couple ways you could do this, varying in the amount
of backflips you need to do to make it work. The cleanest and easiest one
I would suggest would be to use your own PrintDocument class. You could
override the various methods and in addition to using the provided Graphics
object, you could also create a Bitmap and use it's graphics and call the
same methods.

As for the Print button not doing anything in your example. I would guess
that PreviewPrintController doesn't know how to print to an actual printer.
I would go further to say that, internally, the PrintPreviewDialog is
using two controllers, one for the preview and one for the actual print,
but that's just a guess.

Bryan Haber[MSFT]
..NET Client QA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top