none
How to Print a Form in C#

    Question

  • I want to print a 'Customer Order" Form that the user has filled out without having to create a report.  I am new to Windows Apps, (mostly famliar with Asp.net).

    I tried this example and only get a blank page out of the printer:http://msdn.microsoft.com/en-us/library/aa287529(VS.71).aspx

    Does anyone know an easy way to send the current Form to a printer?

    Thanks, Rick

     

    Thursday, April 22, 2010 8:31 PM

Answers

  • Hello rhetherington,
    Welcome to MSDN forum.
    I understand the problem is how to send the current form to a printer.
    As what jayant suggested, PrintDocument Class defines a reusable object that sends output to a printer. So I also think PrintDocument component could be used to solve this problem.
    Here is an example which demonstrates how to print a copy of the current form. Please have a try. I hope it helpful.

    using System;
    using System.Windows.Forms;
    using System.Drawing;
    using System.Drawing.Printing;

    public class Form1 : Form
    {
        private Button printButton = new Button();
        private PrintDocument printDocument1 = new PrintDocument();

        public Form1()
        {
            printButton.Text = "Print Form";
            printButton.Click += new EventHandler(printButton_Click);
            printDocument1.PrintPage +=
                new PrintPageEventHandler(printDocument1_PrintPage);
            this.Controls.Add(printButton);
        }

        void printButton_Click(object sender, EventArgs e)
        {
            CaptureScreen();
            printDocument1.Print();
        }


        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);
        }

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

        public static void Main()
        {
            Application.Run(new Form1());
        }
    }

    More information about PrintDocument Class:
    http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument(VS.71).aspx

    If you have any problems, please feel free to contact me.
    Best regards,
    Liliane


    Please mark the replies as answers if they help and unmark them if they provide no help. Thanks
    • Marked as answer by Liliane Teng Wednesday, April 28, 2010 8:47 AM
    Tuesday, April 27, 2010 2:31 AM
  • have you try the PrintDocument component..?

     

    jayant

    • Marked as answer by Liliane Teng Wednesday, April 28, 2010 8:47 AM
    Friday, April 23, 2010 5:01 AM
  • if  you  want to send content  direclty  to the printer you have to use winspool.drv  please take a look at this  link  for more help
    A man's dreams are an index to his greatness
    • Marked as answer by Liliane Teng Wednesday, April 28, 2010 8:47 AM
    Friday, April 23, 2010 7:39 AM
  • Take a picture of the form and print it using PrintDocument.  To get an image of the Form, use DrawToBitmap if you don't use RichTextBoxes, otherwise use CopyFromScreen.  Use can also use the PowerPacks PrintForm method.
    • Marked as answer by Liliane Teng Wednesday, April 28, 2010 8:47 AM
    Friday, April 23, 2010 7:53 AM

All replies

  • I want to print a 'Customer Order" Form that the user has filled out without having to create a report.  I am new to Windows Apps, (mostly famliar with Asp.net).

    I tried this example and only get a blank page out of the printer:http://msdn.microsoft.com/en-us/library/aa287529(VS.71).aspx

    Does anyone know an easy way to send the current Form to a printer?

    Thanks, Rick

     

    • Merged by Liliane Teng Friday, April 23, 2010 1:50 AM a duplicate thread
    Thursday, April 22, 2010 8:31 PM
  • have you try the PrintDocument component..?

     

    jayant

    • Marked as answer by Liliane Teng Wednesday, April 28, 2010 8:47 AM
    Friday, April 23, 2010 5:01 AM
  • if  you  want to send content  direclty  to the printer you have to use winspool.drv  please take a look at this  link  for more help
    A man's dreams are an index to his greatness
    • Marked as answer by Liliane Teng Wednesday, April 28, 2010 8:47 AM
    Friday, April 23, 2010 7:39 AM
  • Take a picture of the form and print it using PrintDocument.  To get an image of the Form, use DrawToBitmap if you don't use RichTextBoxes, otherwise use CopyFromScreen.  Use can also use the PowerPacks PrintForm method.
    • Marked as answer by Liliane Teng Wednesday, April 28, 2010 8:47 AM
    Friday, April 23, 2010 7:53 AM
  • Hello rhetherington,
    Welcome to MSDN forum.
    I understand the problem is how to send the current form to a printer.
    As what jayant suggested, PrintDocument Class defines a reusable object that sends output to a printer. So I also think PrintDocument component could be used to solve this problem.
    Here is an example which demonstrates how to print a copy of the current form. Please have a try. I hope it helpful.

    using System;
    using System.Windows.Forms;
    using System.Drawing;
    using System.Drawing.Printing;

    public class Form1 : Form
    {
        private Button printButton = new Button();
        private PrintDocument printDocument1 = new PrintDocument();

        public Form1()
        {
            printButton.Text = "Print Form";
            printButton.Click += new EventHandler(printButton_Click);
            printDocument1.PrintPage +=
                new PrintPageEventHandler(printDocument1_PrintPage);
            this.Controls.Add(printButton);
        }

        void printButton_Click(object sender, EventArgs e)
        {
            CaptureScreen();
            printDocument1.Print();
        }


        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);
        }

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

        public static void Main()
        {
            Application.Run(new Form1());
        }
    }

    More information about PrintDocument Class:
    http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument(VS.71).aspx

    If you have any problems, please feel free to contact me.
    Best regards,
    Liliane


    Please mark the replies as answers if they help and unmark them if they provide no help. Thanks
    • Marked as answer by Liliane Teng Wednesday, April 28, 2010 8:47 AM
    Tuesday, April 27, 2010 2:31 AM