Print the Controls of the Window Form

Let us first look at Window Forms.
 
In this form I have taken one picturebox & one button.
 
See the following Image:
 
01_Window_Controls_Print.png
 
Let's look at the code :
  1. [System.Runtime.InteropServices.DllImport("gdi32.dll")]  
In this above code it will import the GDI32 dll.
  1. public static extern long BitBlt(IntPtr Dest, int Xaxes, int Yaxes, int Width, int Height, IntPtr Src, int XSrcaxes, int YSrcaxes, int dwRop);   
To know more about the BitBlt in detail, see this link : BitBlt
 
Create a new object of the PrintDocument for print.
  1. System.Drawing.Printing.PrintDocument myPrintDocument = new System.Drawing.Printing.PrintDocument();  
  1. Graphics myGraphics1 = this.CreateGraphics();  
  2. myBitmap = new Bitmap(this.Width, this.Height, this.CreateGraphics());  
  3. Graphics myGraphics2 = Graphics.FromImage(myBitmap);  
  4. IntPtr HandleDeviceContext1 = myGraphics1.GetHdc();  
  5. IntPtr HandleDeviceContext2 = myGraphics2.GetHdc();  
  6. BitBlt(HandleDeviceContext2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, HandleDeviceContext1, 0, 0, 13369376);  
  7. myGraphics1.ReleaseHdc(HandleDeviceContext1);  
  8. myGraphics2.ReleaseHdc(HandleDeviceContext2);  
The above code is used to print the controls of the Windows Form.
  1. myPrintDocument.PrintPage +=new System.Drawing.Printing.PrintPageEventHandler(myPrintDocument_PrintPage);  
Create the PrintPage Event of PrintDocument Object for Printing.
  1. myPrintDocument.Print(); 
The above code start the printing.
  1. private void myPrintDocument_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)  
  2. {  
  3.     e.Graphics.DrawImage(myBitmap, 0, 0);  
  4. }  
When the PrintPage event fires, the above code will execute.
 
See the whole code:
  1. using System;  
  2. using System.Drawing;  
  3. using System.Windows.Forms;  
  4.   
  5. namespace PrintWindowForm  
  6. {  
  7.     public partial class Form1 : Form  
  8.     {  
  9.         [System.Runtime.InteropServices.DllImport("gdi32.dll")]  
  10.         public static extern long BitBlt(IntPtr Dest, int Xaxes, int Yaxes,   
  11.                            int Width, int Height, IntPtr Src, int XSrcaxes, intYSrcaxes, int dwRop);  
  12.         private Bitmap myBitmap;  
  13.         System.Drawing.Printing.PrintDocument myPrintDocument =   
  14.                 new System.Drawing.Printing.PrintDocument();  
  15.         public Form1()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.         private void myPrintDocument_PrintPage(System.Object sender,   
  20.            System.Drawing.Printing.PrintPageEventArgs e)  
  21.         {  
  22.             e.Graphics.DrawImage(myBitmap, 0, 0);  
  23.         }  
  24.         private void button1_Click(object sender, EventArgs e)  
  25.         {  
  26.             Graphics myGraphics1 = this.CreateGraphics();  
  27.             myBitmap = new Bitmap(this.Width, this.Height, this.CreateGraphics());  
  28.             Graphics myGraphics2 = Graphics.FromImage(myBitmap);  
  29.             IntPtr HandleDeviceContext1 = myGraphics1.GetHdc();  
  30.             IntPtr HandleDeviceContext2 = myGraphics2.GetHdc();  
  31.             BitBlt(HandleDeviceContext2, 0, 0, this.ClientRectangle.Width,   
  32.               this.ClientRectangle.Height, HandleDeviceContext1, 0, 0, 13369376);  
  33.             myGraphics1.ReleaseHdc(HandleDeviceContext1);  
  34.             myGraphics2.ReleaseHdc(HandleDeviceContext2);  
  35.   
  36.             myPrintDocument.PrintPage +=  
  37.                new System.Drawing.Printing.PrintPageEventHandler(myPrintDocument_PrintPage);  
  38.             myPrintDocument.Print();  
  39.         }  
  40.     }  
  41. }  
See the following Image Of Print The Controls Of The Windows Form In OneNote System Printer:
 
02_Window_Controls_Print.png 


Similar Articles