0

I'm trying to print the content of a pictureBox in a MdiChild. Debugging the code looks like it never triggers the PrintPage event. I've mainly used this code for the project: printing content of a picturebox

What is wrong ?

Here is my code:

private void stampaToolStripMenuItem_Click(object sender, EventArgs e)
    {

        Form2 activeChild = this.ActiveMdiChild as Form2;
        PictureBox theBox = (PictureBox)activeChild.pictureBox1;
        dastampare = theBox.Image as Bitmap;
        printDocument1.OriginAtMargins = true;
        printDocument1.DocumentName = "Prova";
        printDialog1.Document = printDocument1;
        printDialog1.ShowDialog();
        if (printDialog1.ShowDialog() == DialogResult.OK)
        {
            printDocument1.Print();
        }
    }

    private void printDocument1_PrintPage(object sender,  System.Drawing.Printing.PrintPageEventArgs e)
    {
        e.Graphics.DrawImage(dastampare, 0, 0);
    }
Community
  • 1
  • 1
Podarce
  • 13
  • 2
  • 1
    Note: You are calling `printDialog1.ShowDialog()` twice. If your debugger is reaching the `printDocument1.Print();` line and not your PrintPage event, then you most likely don't have the event handler wired up. – LarsTech Feb 05 '13 at 22:42
  • You are absolutely right LarsTech. Fixed it removing printDialog1.ShowDialog(); Still I can't trigger the PrintPage event. :( – Podarce Feb 05 '13 at 23:19

1 Answers1

0

In your form's constructor, try wiring up the eventhandler:

public Form1() {
  InitializeComponent();
  printDocument1.PrintPage += printDocument1_PrintPage;
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225