Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
its very urgent... i m working on database application n i want to print a complete
windows application form.
Posted

There are two ways to do this:
Quick and dirty: The PrintForm[^] class

A bit slower to implement: the PrintDocument[^] class.

The former generally produces a nasty print as it does just what it says on the tin: prints a copy of the form.
The latter is more work to implement, but produces a much, much better result and is a lot more flexible.

I do not use the former; once you are familiar with the latter you will see why!
 
Share this answer
 
Comments
ridoy 16-Oct-12 9:51am    
+5
Hello,

Hope this will helps you.
C#
// Handler for print call
private void ButtonPrintClick(object sender, EventArgs e)
{
	// Create document
	PrintDocument _document = new PrintDocument();
	// Add print handler
	_document.PrintPage += new PrintPageEventHandler(Document_PrintPage);
	// Create the dialog to display results
	PrintPreviewDialog _dlg = new PrintPreviewDialog();
	_dlg.ClientSize = new System.Drawing.Size(Width / 2, Height / 2);
	_dlg.Location = new System.Drawing.Point(Left, Top);
	_dlg.MinimumSize = new System.Drawing.Size(375, 250);
	_dlg.UseAntiAlias = true;
	// Setting up our document
	_dlg.Document = _document;
	// Show it
	_dlg.ShowDialog(this);
	// Dispose document
	_document.Dispose();
}
// Print handler
private void Document_PrintPage(object sender, PrintPageEventArgs e)
{
	// Create Bitmap according form size
	Bitmap _bitmap = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
	// Draw from into Bitmap DC
	this.DrawToBitmap(_bitmap, this.DisplayRectangle);
	// Draw Bitmap into Printer DC
	e.Graphics.DrawImage(_bitmap,0,0);
	// No longer deeded - dispose it
	_bitmap.Dispose();
}

Regards,
Maxim.
 
Share this answer
 
Comments
ridoy 16-Oct-12 9:51am    
+5
 
Share this answer
 
Add the following class to your code. Then call the function CaptureImage as specified below from you code:

C#
class ScreenShot
{
    public static void CaptureImage(Point SourcePoint, Point DestinationPoint, Rectangle SelectionRectangle, string FilePath)
    {
        using (Bitmap bitmap = new Bitmap(SelectionRectangle.Width, SelectionRectangle.Height))
        {
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                g.CopyFromScreen(SourcePoint, DestinationPoint, SelectionRectangle.Size);
            }
            bitmap.Save(FilePath, ImageFormat.Bmp);
        }
    }
}


Call function CaptureImage as below (here this referes to the form, you can replace the form object):

C#
private void button1_Click(object sender, EventArgs e)
{
    ScreenShot.CaptureImage(this.Location, new Point(this.Location.X + this.Width, this.Location.Y+this.Height), new Rectangle(this.Location.X,this.Location.Y,this.Width,this.Height), @"c:\zyx.bmp");
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900