我在 C# Winforms 应用程序中有一个面板,我需要在多个页面中打印它的内容。我使用了以下代码,我知道它在某处需要修改,请帮助我...
private void PrintP_Load(object sender, EventArgs e)
{
tableLayoutPanel1.AutoSize = false; ;
tableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
Print(this.panel2);
this.Close();
}
Bitmap MemoryImage;
int page=0;
public void GetPrintArea(Panel pnl)
{
page = 0;
MemoryImage = new Bitmap(pnl.Width, pnl.Height);
Rectangle rect = new Rectangle(0, 0, pnl.Width, pnl.Height);
pnl.DrawToBitmap(MemoryImage, new Rectangle(0, 0, pnl.Width, pnl.Height));
}
protected override void OnPaint(PaintEventArgs e)
{
if (MemoryImage != null)
{
e.Graphics.DrawImage(MemoryImage, 0, 0);
base.OnPaint(e);
}
}
void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
var pagearea = e.PageBounds;
e.Graphics.DrawImage(MemoryImage, new Rectangle(e.PageBounds.Location, e.PageBounds.Size), new Rectangle(e.PageBounds.Location.X, e.PageBounds.Height * page, e.PageBounds.Width, e.PageBounds.Height), GraphicsUnit.Pixel);
e.HasMorePages = e.PageBounds.Height * page < panel2.Size.Height;
page++;
}
public void Print(Panel pnl)
{
panel2 = pnl;
GetPrintArea(pnl);
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
}