嗨,我从 MSDN 获得了此代码。我无法弄清楚问题所在,希望您能提供帮助。错误位于带有 printForm1 字样的两行中。我的表单实际上称为 Form1,所以我看不到问题所在。
[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 btnCreate_Click(object sender, EventArgs e)
{
btncustCont.Visible = false;
btnprintCost.Visible = false;
btnpdfCont.Visible = false;
btnpdfBrochureCont.Visible = false;
btnpaperCont.Visible = false;
btnduplicateCont.Visible = false;
btntypeCont.Visible = false;
btnCalcCost.Visible = false;
btnprintCost.Visible = false;
Application.DoEvents();
CaptureScreen();
printForm1.PrinterSettings.DefaultPageSettings.Landscape = true;
printForm1.Print();
btncustCont.Visible = true;
btnprintCost.Visible = true;
btnpdfCont.Visible = true;
btnpdfBrochureCont.Visible = true;
btnpaperCont.Visible = true;
btnduplicateCont.Visible = true;
btntypeCont.Visible = true;
btnCalcCost.Visible = true;
btnprintCost.Visible = true;
}