Print a Windows Form in C#
This article describes how to print a Windows Form. The concept is capturing the screen image of the Windows Form in a Bitmap and printing it.
So, let's create this simple Windows Form then drag a printDocument control to the form:
First add this directive
Then add this code:
And in printButton click event add this code:
So, let's create this simple Windows Form then drag a printDocument control to the form:
First add this directive
:
using System.Drawing.Printing;
Then add this code:
Bitmap memorizedImage;
private void CaptureScreen()
{
Graphics myGraphics = this.CreateGraphics();
Size s = this.Size;
memorizedImage = new Bitmap(s.Width, s.Height, myGraphics);
Graphics memoryGraphics = Graphics.FromImage(memorizedImage);
memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
}
private void printDocument1_PrintPage(System.Object sender,System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(memorizedImage, 0, 0);
}
And in printButton click event add this code:
private void printButton_Click(object sender, EventArgs e)
{
CaptureScreen();
printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
printDocument1.Print();
}
Print a Windows Form in C#
Reviewed by Bloggeur DZ
on
03:25
Rating:
Aucun commentaire: