Show print dialog before printing

I want to show the print dialog before printing the document so that the user can select a different printer before printing. Code for printing:

private void button1_Click(object sender, EventArgs e) { try { PrintDocument pd = new PrintDocument(); pd.PrintPage += new PrintPageEventHandler(PrintImage); pd.Print(); } catch (Exception ex) { MessageBox.Show(ex.Message, ToString()); } } void PrintImage(object o, PrintPageEventArgs e) { int x = SystemInformation.WorkingArea.X; int y = SystemInformation.WorkingArea.Y; int width = this.Width; int height = this.Height; Rectangle bounds = new Rectangle(x, y, width, height); Bitmap img = new Bitmap(width, height); this.DrawToBitmap(img, bounds); Point p = new Point(100, 100); e.Graphics.DrawImage(img, p); } 

Can this code print the current form?

+7
source share
2 answers

You must use PrintDialog

  PrintDocument pd = new PrintDocument(); pd.PrintPage += new PrintPageEventHandler(PrintPage); PrintDialog pdi = new PrintDialog(); pdi.Document = pd; if (pdi.ShowDialog() == DialogResult.OK) { pd.Print(); } else { MessageBox.Show("Print Cancelled"); } 

Edited (from comment)

On 64-bit Windows and with some versions of .NET, you may need to set pdi.UseExDialog = true ; to display a dialog box.

+15
source

To complete the code, the code should contain a usage directive

 using System.Drawing.Printing; 

for further links please go to PrintDocument Class

+1
source

All Articles