Reader Level:
Articles

Print the Controls of the Window Form

By Ghanashyam Nayak on February 13, 2012
This article will show you how you can print all the controls of a Window Form.
  • 0
  • 0
  • 7961
Download Files:
 

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 :
 

 [System.Runtime.InteropServices.DllImport("gdi32.dll")]

In this above code it will import the GDI32 dll.
 

 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.
 

 System.Drawing.Printing.PrintDocument myPrintDocument = new System.Drawing.Printing.PrintDocument();

 

Graphics myGraphics1 = this.CreateGraphics();
myBitmap = 
new Bitmap(this.Width, this.Height, this.CreateGraphics());
Graphics myGraphics2 = Graphics.FromImage(myBitmap);
IntPtr HandleDeviceContext1 = myGraphics1.GetHdc();
IntPtr HandleDeviceContext2 = myGraphics2.GetHdc();
BitBlt(HandleDeviceContext2, 0, 0, 
this.ClientRectangle.Width, this.ClientRectangle.Height, HandleDeviceContext1, 0, 0, 13369376);
myGraphics1.ReleaseHdc(HandleDeviceContext1);
myGraphics2.ReleaseHdc(HandleDeviceContext2);

The above code is used to print the controls of the Windows Form.
 

 myPrintDocument.PrintPage +=new System.Drawing.Printing.PrintPageEventHandler(myPrintDocument_PrintPage);

Create the PrintPage Event of PrintDocument Object for Printing.
 

 myPrintDocument.Print();

The above code start the printing.
 

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

When the PrintPage event fires, the above code will execute.

See the whole code:
 

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

namespace PrintWindowForm
{
    
public partial class Form1 : Form
    {
        [System.Runtime.InteropServices.DllImport("gdi32.dll")]
        
public static extern long BitBlt(IntPtr Dest, int Xaxes, int Yaxes, 
                          
int Width, int Height, IntPtr Src, int XSrcaxes, intYSrcaxes, int dwRop);
        
private Bitmap myBitmap;
        System.Drawing.Printing.PrintDocument myPrintDocument = 

                new
 System.Drawing.Printing.PrintDocument();
        
public Form1()
        {
            InitializeComponent();
        }
        
private void myPrintDocument_PrintPage(System.Object sender,
           System.Drawing.Printing.PrintPageEventArgs e)
        {
            e.Graphics.DrawImage(myBitmap, 0, 0);
        }
        
private void button1_Click(object sender, EventArgs e)
        {
            Graphics myGraphics1 = 
this.CreateGraphics();
            myBitmap = 
new Bitmap(this.Width, this.Height, this.CreateGraphics());
            
Graphics myGraphics2 = Graphics.FromImage(myBitmap);
            
IntPtr HandleDeviceContext1 = myGraphics1.GetHdc();
            
IntPtr HandleDeviceContext2 = myGraphics2.GetHdc();
            BitBlt(HandleDeviceContext2, 0, 0, 
this.ClientRectangle.Width, 
              this
.ClientRectangle.Height, HandleDeviceContext1, 0, 0, 13369376);
            myGraphics1.ReleaseHdc(HandleDeviceContext1);
            myGraphics2.ReleaseHdc(HandleDeviceContext2);

            myPrintDocument.PrintPage +=
               new
 System.Drawing.Printing.PrintPageEventHandler(myPrintDocument_PrintPage);
            myPrintDocument.Print();
        }
    }
}

See the following Image Of Print The Controls Of The Windows Form In OneNote System Printer:

  02_Window_Controls_Print.png

Ghanashyam Nayak

To know more about me, you can follow me on Facebook :http://www.facebook.com/nayak.ghanashyam

COMMENT USING

Trending up