Dotnet-StuffWorld

Sunday, August 23, 2009

 

How to see Print Preview & Print a WindowsForm



using System.Drawing.Printing; //For "PrintDocument" Class

public partial class Form3 : Form
{
PrintDocument PrintDoc1 = new PrintDocument();
PrintPreviewDialog PPDlg1 = new PrintPreviewDialog();

public Form3()
{
InitializeComponent();

PPDlg1.Document = PrintDoc1;
PrintDoc1.OriginAtMargins =true; //To set or Get the Position of a Graphic Object
PrintDoc1.PrintPage += PDoc_PrintPage;
}

private void btn_PrintPreview_Click(object sender, EventArgs e)
{// When PrintPreview Button Clicks
PPDlg1.ShowDialog();
}

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);
}

private void btn_PrintPage_Click(object sender, EventArgs e)
{//When Print Button Clicks, Image will be show & Ready to Print
PrintDoc1.Print();
}

}

Comments:

Post a Comment

Subscribe to Post Comments [Atom]





<< Home

Archives

April 2009   July 2009   August 2009   September 2009   September 2010   February 2011  

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]