Print the Window Form & Print the Image of Picture Box

Let's first look at the form; see the following image :
 
01_Print_Form_Picture.png
 
Now the code:
 
In the form there are two options. First one is to print the whole form & the other one is to print the picture.
 
Let us see first how to print the picture.
 
First of all we have to create an object of Bitmap & its size is as picturebox width & height.
  1. Bitmap myBitmap1 = new Bitmap(myPicturebox.Width, myPicturebox.Height);  
Now Print this picturebox image.
  1. myPicturebox.DrawToBitmap(myBitmap1, new Rectangle(0, 0, myPicturebox.Width, myPicturebox.Height));    
  2. e.Graphics.DrawImage(myBitmap1, 0, 0);  
Then dispose the bitmap to release the resources.
 
Now create the objects of the PrintDocument & PrintDialog.
  1. System.Drawing.Printing.PrintDocument myPrintDocument1 = new System.Drawing.Printing.PrintDocument();  
  2. PrintDialog myPrinDialog1 = new PrintDialog();  
Now create the PagePrint event of PrintDocument
  1. myPrintDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(myPrintDocument2_PrintPage);  
Then accessing the PrintDocument object to PrintDialog.
  1. myPrinDialog1.Document = myPrintDocument1;  
Finally it will print the Image which is in the picturebox.
  1. if (myPrinDialog1.ShowDialog() == DialogResult.OK)  
  2. {  
  3.     myPrintDocument1.Print();  
  4. }   
The above code will ask you the printer name in which you can print the picture.
 
The full code for printing the picture.
  1. private void myPrintDocument2_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)  
  2. {  
  3.  Bitmap myBitmap1 = new Bitmap(myPicturebox.Width, myPicturebox.Height);  
  4.  myPicturebox.DrawToBitmap(myBitmap1, new Rectangle(0, 0, myPicturebox.Width, myPicturebox.Height));  
  5.  e.Graphics.DrawImage(myBitmap1, 0, 0);  
  6.  myBitmap1.Dispose();  
  7. }  
  8. private void btnPrintPicture_Click(object sender, EventArgs e)  
  9. {  
  10.  System.Drawing.Printing.PrintDocument myPrintDocument1 = new System.Drawing.Printing.PrintDocument();  
  11.  PrintDialog myPrinDialog1 = new PrintDialog();  
  12.  myPrintDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(myPrintDocument2_PrintPage);  
  13.  myPrinDialog1.Document = myPrintDocument1;  
  14.  if (myPrinDialog1.ShowDialog() == DialogResult.OK)  
  15.  {  
  16.   myPrintDocument1.Print();  
  17.  }  
  18. }  
See the following images :
 
02_Print_Form_Picture.png
 
03_Print_Form_Picture.png

04_Print_Form_Picture.png

The same code will be used for the whole form but instead of the pictureboc object it will use this & using the width & height, it will print the whole form.
 
The whole code for printing the form:
  1. private void myPrintDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)  
  2. {  
  3.  Bitmap myBitmap2 = new Bitmap(this.Width, this.Height);  
  4.  this.DrawToBitmap(myBitmap2, new Rectangle(0, 0, this.Width, this.Height));  
  5.  e.Graphics.DrawImage(myBitmap2, 0, 0);  
  6.  myBitmap2.Dispose();  
  7. }  
  8. private void btnPrintForm_Click(object sender, EventArgs e)  
  9. {  
  10.  System.Drawing.Printing.PrintDocument myPrintDocument2 = new System.Drawing.Printing.PrintDocument();  
  11.  PrintDialog myPrinDialog2 = new PrintDialog();  
  12.  myPrintDocument2.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(myPrintDocument1_PrintPage);  
  13.  myPrinDialog2.Document = myPrintDocument2;  
  14.  if (myPrinDialog2.ShowDialog() == DialogResult.OK)  
  15.  {  
  16.   myPrintDocument2.Print();  
  17.  }  
  18. }  
See the following images :
 
05_Print_Form_Picture.png
06_Print_Form_Picture.png 
Hope this is clear to all of you….