Create an a4 sized form & print it? / Add header, footer/ Printingform Component


  • I want to print an a4 sized form since the a4 size is to big it makes the form disapear under the taskbar, and when it's printed out i can see the taskbar on the paper. I guess it makes a screenshoot over the form?

    And i need to add, the text is kind of blurred, and it sometimes misses to print thin lines. Looks good on the computer, not so good on the paper.

    I use this code; (source msdn)

            private void CaptureScreen()
                Graphics mygraphics = this.CreateGraphics();
                Size s = this.Size;
                memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
                Graphics memoryGraphics = Graphics.FromImage(memoryImage);
                IntPtr dc1 = mygraphics.GetHdc();
                IntPtr dc2 = memoryGraphics.GetHdc();
                BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);

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

    and to fire it off;

    Thans in advance!

    • Edited by thrinker Tuesday, February 10, 2009 9:28 PM
    Tuesday, February 10, 2009 3:15 PM


All replies

  • Try DrawToBitmap.
    Tuesday, February 10, 2009 4:47 PM
  • Hi, I tryed to google drawtobitmap and i found a codesample, i made a picturebox, large enough to fit the whole form. I used this code to provide a image:

    pictureBox3.BackgroundImage = GetFormImage(this);

    It takes a picture, but its not on the form, its whats behind it. I made a printscreen:

    Any ideas?

    public Bitmap GetFormImage(Form frm)

                Bitmap bmp = null;

                Graphics formGraphics = null;

                Graphics memoryGraphics = null;

                IntPtr dc1 = IntPtr.Zero;

                IntPtr dc2 = IntPtr.Zero;


                    formGraphics = frm.CreateGraphics();

                    //  Create a bitmap big enough to contain

                    //  the entire form, and retrieve a Graphics

                    //  object that can work with the bitmap:

                    Size sz = frm.Size;

                    bmp = new Bitmap(sz.Width, sz.Height, formGraphics);

                    memoryGraphics = Graphics.FromImage(bmp);

                    //  Retrieve the two drawing context handles (HDCs)

                    //  dc1 represents the form

                    //  dc2 represents the new bitmap

                    dc1 = formGraphics.GetHdc();

                    dc2 = memoryGraphics.GetHdc();

                    //  Copy the image to dc2,

                    //  using rectange 0, 0, sz.Width, sz.Height

                    //  from dc1

                    //  copy from the upper-left corner

                    BitBlt(dc2, 0, 0, sz.Width, sz.Height, dc1, ((1 * SystemInformation.FrameBorderSize.Width)

                                    * -1), ((1

                                    * (SystemInformation.FrameBorderSize.Height + SystemInformation.CaptionHeight))

                                    * -1), SRCCOPY);


                catch (Exception ex)




                    if (!(formGraphics == null))




                    if (!(memoryGraphics == null))





                return bmp;

    Tuesday, February 10, 2009 6:32 PM
  • I have no idea what you are doing.  Use DrawToBitmap:

          Bitmap bmp = new Bitmap(this.Width, this.Height);  
          this.DrawToBitmap(bmp, new Rectangle(0,0,bmp.Width,bmp.Height);  

    Crop the bitmap to the area you want by cloning it.
    • Edited by JohnWein Tuesday, February 10, 2009 11:23 PM
    Tuesday, February 10, 2009 7:43 PM
  • Ok, I got a little further, I installed the printform component from microsoft.

    I am able to print now, but I want to add a header and a footer to my document.

    Any ideas how?

    Thanks in advance!
    Tuesday, February 10, 2009 9:27 PM
  • What version of Visual Studio are you using?  PrintForm  is installed as a part of VS2008.  How are you using PrintForm and PrintDocument  together?
    Tuesday, February 10, 2009 10:35 PM
  • Oh darn, yea i use VS2008, but i went to that page, installed the component and it displayed in the toolbox, under 'Visual Basic PowerPacks'

    I'm totaly lost, isnt there a way to add a header,container,footer? I'm trying to make a reciept.

    Anyway, thanks..
    Tuesday, February 10, 2009 10:42 PM
  • What problem do you have with DrawToBitmap?
    Tuesday, February 10, 2009 10:50 PM
  • But your example isnt c# is it? :/
    Tuesday, February 10, 2009 10:56 PM
  • It is now.
    Tuesday, February 10, 2009 11:24 PM
  • I tryed using this code:

    System.Drawing.Image printImage;

    Bitmap bmp = new Bitmap(this.Width, this.Height);
    this.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));

    printImage = bmp;


    It printed the page once, then it said the file was missing. But when it printed the page it was just a blank form. Couldnt see my labels and other stuffs on it.

    Wednesday, February 11, 2009 11:10 AM
  • Hi thrinker,

    As far as I know, you can use PrintForm Component or Control.DrawToBitmap Method or other ways to do so.
    I. Using PrintForm Component
    1. Print Windows Forms w/o using API, which provids sample project about printing Windows Forms using 100% managed code.
    2. All about PrintForm component 1.0, which discusses printing a Form in VB.NET. PrintForm Component (Visual Basic)
    3. Other Form Print ControlFormPrint as a simple class.

    II. Using Control.DrawToBitmap Method
      Take a look at capturing a panel to a jpg.

    III. Using other ways
    1. Screen Capture and Save as an Image
    2. How To: Printing Form Controls in C# and .NET
    3. Capture a Screen Shot

    Have a try.

    Best regards,

    • Marked as answer by Guo Surfer Tuesday, February 17, 2009 2:31 AM
    Friday, February 13, 2009 11:43 AM