0

Good evening all, I am currently stuck with a little formatting problem whilst printing my form. The code I am using is activated via a button ("Print Form") and looks like this:

private void printAdmin_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        printAdmin.DefaultPageSettings.Landscape = true;

        e.Graphics.DrawImage(AdminPage,0,0);
    }
    Bitmap AdminPage;
    private void Btn_Admin_Prt_Click(object sender, EventArgs e)
    {
        try
        {

            Graphics g = this.CreateGraphics();
            AdminPage = new Bitmap(Size.Width, Size.Height, g);
            Graphics Printed = Graphics.FromImage(AdminPage);
            Printed.CopyFromScreen(this.Location.X, this.Location.Y,0,0,this.Size);

            printPreviewAdminDialogue.ShowDialog();
        }
        catch(Exception )
        {
            MessageBox.Show("Please check printer connection!");
        }

        //Takes a screenshot of the admin page
        //opens print dialogue
        //prints awesomley
    }

When I run the application there are no errors until it comes to the print preview, this is supposed to print an entire screenshot of the form however cuts half of it off and looks like the image below. Image of print preview

Any ideas on how to format this correctly so the entire form is captured.

Thanks in advance.

2185069
  • 31
  • 5

1 Answers1

0

It's just a matter of measuring the right coordinates.
You need a single Graphics object to capture the screen region and transfer it to a Bitmap (a Graphics object that actually relates to the Bitmap).

I didn't know whether you needed just the ClientArea or the whole Form, so this method accepts a bool parameter, TheWholeForm, that lets you specify which one.

Bitmap AdminPage_Full;
Bitmap AdminPage_ClientArea;

    AdminPage_Full = PrintForm(true);
    AdminPage_ClientArea = PrintForm(false);

    //Test it
    this.pictureBox1.Image = (Bitmap)AdminPage_Full.Clone();
    this.pictureBox2.Image = (Bitmap)AdminPage_ClientArea.Clone();

    private Bitmap PrintForm(bool TheWholeForm)
    {
        using (Bitmap bitmap = new Bitmap(this.Bounds.Width, this.Bounds.Height)) {
            using (Graphics graphics = Graphics.FromImage(bitmap)) {
                if (TheWholeForm) {
                    graphics.CopyFromScreen(this.Bounds.Location, new Point(0, 0), this.Bounds.Size);
                } else {
                    graphics.CopyFromScreen(this.PointToScreen(this.ClientRectangle.Location),
                                            new Point(0, 0), this.ClientRectangle.Size);
                }
                graphics.DrawImage(bitmap, new Point(0, 0));
                return (Bitmap)bitmap.Clone();
            };
        };
    }
Jimi
  • 29,621
  • 8
  • 43
  • 61