ARTICLE

Print the Controls of the Window Form

Posted by Ghanashyam Nayak Articles | Printing in C# February 13, 2012
This article will show you how you can print all the controls of a Window Form.
Reader Level:
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

Login to add your contents and source code to this article
post comment
     

Nice share Man

Posted by Sivaraman Dhamodaran Jun 14, 2012

That was a cool idea! Thanks for sharing Ghanshyam.

Posted by Jean Paul Jun 13, 2012

Hi Rikin, For that you have you use the X & Y coordinates. you have to find the starting point of that blank values & then you have to print each your values at that related coordinates.

Posted by Ghanashyam Nayak May 21, 2012

Hello, I need your kind help. I have 1 task in Printing a winform's values. Client has given already printed paper with blank value. these values i need to fill in paper while printing. Pls give me code that can print only values at specific locations. that would be better if this can be possible directly from Current form or using Report Viewer. pls help me in this with code.

Posted by rikin shah May 12, 2012

Hello, I need your kind help. I have 1 task in Printing a winform's values. Client has given already printed paper with blank value. these values i need to fill in paper while printing. Pls give me code that can print only values at specific locations. that would be better if this can be possible directly from Current form or using Report Viewer. pls help me in this with code.

Posted by rikin shah May 12, 2012
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.