Sunday 8 May 2011

Printing Controls in C#

Printing Controls
These controls are used to take printing of documents whether in form of print screen or as printing in the content form .
In following figure i tried to describe about these controls how you will use in your window application.
For explaining this i had taken the previous example of dialog control, so lets proceed  with printing controls.


Code for Print Preview- For performing this action first add namespace-

using System.Drawing.Printing;

        PrintDocument PrintDoc1 = new PrintDocument();
        PrintPreviewDialog PrintPreviewDialog1 = new PrintPreviewDialog();
        private void printToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PrintPreviewDialog1.Document = PrintDoc1;
            PrintDoc1.OriginAtMargins = true; //To set or Get the Position of a Graphic Object
            PrintDoc1.PrintPage += PDoc_PrintPage;
            PrintPreviewDialog1.ShowDialog();            
        }
Create Method

private void PDoc_PrintPage(object sender, PrintPageEventArgs e)
        {
            Bitmap bmp = new Bitmap(this.Width, this.Height);
            this.DrawToBitmap(bmp, this.ClientRectangle);
            this.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height)); //Takes the Snap of 
the Exact WindowForm size as Bitmap image
            e.Graphics.DrawImage(bmp, 0, 0);
        }
 
Code for taking Print

private void printToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            
            PrintDoc1.Print();
        }

                        

No comments:

Post a Comment