简体   繁体   中英

printing out a Form

I'm trying to print a form: (it has a tablelauyout with 4 colomns on top another tablelayoutpanel with there on top some labels that are needed to print) but I only get a blank page:

    public void PrintPanel()
    {
        PrintDocument printdoc = new PrintDocument();
        PrintDialog myPrintDialog = new PrintDialog();
        memoryImage = new Bitmap(this.Width, this.Height);
        this.tableLayoutPanel1.DrawToBitmap(memoryImage, this.ClientRectangle);
        if (myPrintDialog.ShowDialog() == DialogResult.OK)
        {
            printdoc.PrintPage += printdoc_PrintPage;
            System.Drawing.Printing.PrinterSettings values;
            values = myPrintDialog.PrinterSettings;
            myPrintDialog.Document = printdoc;
            printdoc.PrintController = new StandardPrintController();
            printdoc.Print();
        }
        memoryImage.Dispose();
        printdoc.Dispose();
    }

    void printdoc_PrintPage(object sender, PrintPageEventArgs e)
    {
        e.Graphics.DrawImage(memoryImage, 10, 10);
    }

so I saved the 'memoryImage' to a folder but this is also blank, so what do I wrong? The form is not called with .Show() whene I show it, the code print correctly, but I want to avoid the users see this.

Take a PrintDocument control and place it to your form and call printDocument1_PrintPage event.

Bitmap img;

private void btnPrintForm_Click(object sender, EventArgs e)
{
    CaptureScreen();
    printDocument1.Print();
    printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
}

private void CaptureScreen()
{
    Graphics g = this.CreateGraphics();
    Size s = this.Size;
    img = new Bitmap(s.Width, s.Height, g);
    Graphics mg = Graphics.FromImage(img);
    mg.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
}

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    e.Graphics.DrawImage(img, 0, 0);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM