Karl

Karl

  • NA
  • 6
  • 0

Printing a Windows Form to fit A4 paper

Feb 14 2010 5:28 PM

This is my first post and I apologise if I'm using the wrong thread.

I'm trying to print out a form which is working but its real size where I really want it to fill the page.

My code that I'm using to print is below (as you've probably seen). Can anyone tell me what I need to add or edit to make the form I'm printing fill a A4 page? (well, up to the margins).

Actually, if anyone would be able to tell me how I could edit the CaptureScreen method to trim the form that would be great too.

Thanks in advance.

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Drawing.Printing;


namespace Print_Sample

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }


        private void btnPrint_Click(object sender, EventArgs e)

        {

            PrintDocument document = new PrintDocument();

            document.PrintPage += new PrintPageEventHandler(document_PrintPage);

            CaptureScreen();

            document.Print();

        }


        void document_PrintPage(object sender, PrintPageEventArgs e)

        {

            e.Graphics.DrawImage(memoryImage, 0, 0);

        }


        Bitmap memoryImage;


        private void CaptureScreen()

        {

            Graphics myGraphics = this.CreateGraphics();

            Size s = this.Size;

            memoryImage = new Bitmap(s.Width, s.Height, myGraphics);

            Graphics memoryGraphics = Graphics.FromImage(memoryImage);

            memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);

        }


    }

}



Answers (3)