Print Dialog And Print Preview Tools Using Entire Windows Form To Print

In this article, we will discuss how to print an entire Windows Form using Print Dialog & Print preview tools in a Windows Forms application, step-by-step. It displays the actual printing page size and print area and print page type (single, double) in the the entire designed form.

Print Preview tool

It is an open tool and supports all the Visual Studio versions. We use this tool to show the print preview of the page. The PrintPreviewDialog is convenient and easy to use. All you need to do is to create an instance of the dialog class and assign your Print Document object to the Document property.

Print Dialog tool

This tool helps to print the dialog control that is used to open the Windows Print Dialog and let the user select the printer, set printer, and paper properties to print a file.

Let's begin.

Step 1

Click New >> Project >> Visual C# >> Windows >> Windows Forms Application. Enter your Project name and click OK. 

 

Step 2

Click the View->Select Toolbox. We are using this Toolbox to design the Form in Windows application and Add Print (Button).



Step 3

In Toolbox, we have to add Print Dialog & Print preview. Just Drag & Drop in the form.

 

Step 4

Double Click the Print Document properties and printDocument1_PrintPage to be completed for the event to make the function more accurate.

C# Code 

  1. private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)  
  2. {  
  3.    e.Graphics.DrawImage(bitmap, 0, 0);  
  4. }  
Step 5

Double click the Print button and button2_Click to be completed for the event to make the function more accurate.

Header File

using System.Drawing.Printing;

C# Code

  1. private void button2_Click(object sender, EventArgs e) {  
  2.     Panel panel = new Panel();  
  3.     this.Controls.Add(panel);  
  4.     Graphics grp = panel.CreateGraphics();  
  5.     Size formSize = this.ClientSize;  
  6.     bitmap = new Bitmap(formSize.Width, formSize.Height, grp);  
  7.     grp = Graphics.FromImage(bitmap);  
  8.     Point panelLocation = PointToScreen(panel.Location);  
  9.     grp.CopyFromScreen(panelLocation.X, panelLocation.Y, 0, 0, formSize);  
  10.     printPreviewDialog1.Document = printDocument1;  
  11.     printPreviewDialog1.PrintPreviewControl.Zoom = 1;  
  12.     printPreviewDialog1.ShowDialog();  
  13. }  
  14. Bitmap bitmap;  
  15. private void CaptureScreen() {  
  16.     Graphics myGraphics = this.CreateGraphics();  
  17.     Size s = this.Size;  
  18.     bitmap = new Bitmap(s.Width, s.Height, myGraphics);  
  19.     Graphics memoryGraphics = Graphics.FromImage(bitmap);  
  20.     memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);  
  21. }  
Step 6

At Runtime, the dialog will show generating previews like given below. It will show the time of printing also.

 

Step 7 

Press F5 or "Build and Run" the application.



Finally, we have successfully printed the entire Windows Forms application using Print dialog and Print Preview.