C#打印panel控件中内容 分页的问题
作了一个Winform程序。可以通过查询数据库提取图片、文字信息显示在一个panel中。信息都是动态的。但是都是显示在一个自动伸展的Panel中。
打印的代码如下。
this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(pd_PrintPage);
private void BtnPrint_Click(object sender, EventArgs e)
{
Graphics graphic = panelReports.CreateGraphics();
Size s = panelReports.Size;
Bitmap sourceBitmap = new Bitmap(s.Width, s.Height);
Bitmap memImage = new Bitmap(s.Width, s.Height, graphic);
Graphics memGraphic = Graphics.FromImage(memImage);
IntPtr dc1 = graphic.GetHdc();
IntPtr dc2 = memGraphic.GetHdc();
BitBlt(dc2, 0, 0, panelReports.ClientRectangle.Width, panelReports.ClientRectangle.Height,
dc1, 0, 0, 13369376);
Image print_image = (Image)memImage.Clone();
graphic.ReleaseHdc(dc1);
memGraphic.ReleaseHdc(dc2);
graphic.Dispose();
memGraphic.Dispose();
//memImage.Dispose();
PrintPreviewDialog dlg = new PrintPreviewDialog();
dlg.Width = s.Width;
dlg.Height = s.Height;
dlg.Document = printDocument1;
pageSetupDialog1.Document = printDocument1;
pageSetupDialog1.ShowDialog();
if (dlg.ShowDialog() == DialogResult.OK)
printDocument1.Print();
}
void pd_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Bitmap bmp = new Bitmap(this.panelReports.Width, this.panelReports.Height);
this.panelReports.DrawToBitmap(bmp, new Rectangle(Point.Empty, this.panelReports.Size));
e.Graphics.DrawImage(bmp, Point.Empty);
}
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]//应用API函数
private static extern bool BitBlt(
IntPtr hdcDest, // 目标设备的句柄
int nXDest, // 目标对象的左上角的X坐标
int nYDest, // 目标对象的左上角的X坐标
int nWidth, // 目标对象的矩形的宽度
int nHeight, // 目标对象的矩形的长度
IntPtr hdcSrc, // 源设备的句柄
int nXSrc, // 源对象的左上角的X坐标
int nYSrc, // 源对象的左上角的X坐标
System.Int32 dwRop // 光栅的操作值
);
打印功能可以实现了。但是内容多的时候不知道怎么实现分页。需求要求是A4纸宽度。一半14高度。
打印这段代码是看网上的教程弄的。熟悉的朋友帮忙解说下。或者有更好的可以实现分页打印的方法帮忙提供下。谢谢