Click here to Skip to main content
15,883,749 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Windows form that has 4 pages. My form has text boxes with values, and labels. I and trying to save the form and print it. I am able to print only the first page. How can I print the other pages? When I try to save it, I get the blank page. The code I am using is below: Thanks.

What I have tried:

[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern long BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
private Bitmap memoryImage;

private void CaptureScreen()
{
Graphics mygraphics = this.CreateGraphics();
Size s = this.Size;
memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
IntPtr dc1 = mygraphics.GetHdc();
IntPtr dc2 = memoryGraphics.GetHdc();
BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
mygraphics.ReleaseHdc(dc1);
memoryGraphics.ReleaseHdc(dc2);
}

private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 0, 0);
}

private void printToolStripButton_Click(object sender, EventArgs e)
{
CaptureScreen();
printDocument1.Print();
}


private void saveToolStripButton_Click(object sender, EventArgs e)
{
Stream myStream;
saveFileDialog1.Filter = "All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
using (var writer = new StreamWriter(myStream))
{
CaptureScreen();
writer.WriteLine();
}
myStream.Close();
}
}
}
Posted
Updated 26-Sep-16 4:19am
Comments
Philippe Mori 26-Sep-16 12:53pm    
Put your code in a code block.

1 solution

Don't do it like that!
Instead of taking a screen image, print the content of the form - the data it is displaying in the various controls - directly onto your PrintDocument.
PrintDocument Class (System.Drawing.Printing)[^] includes a basic example.
 
Share this answer
 
Comments
Manoj Kumar Choubey 26-Sep-16 10:48am    
+5

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