C#打印panel控件中内容 分页的问题

k6693075 2012-04-23 11:53:47
作了一个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高度。
打印这段代码是看网上的教程弄的。熟悉的朋友帮忙解说下。或者有更好的可以实现分页打印的方法帮忙提供下。谢谢
...全文
462 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
大胡子陈工 2014-10-12
  • 打赏
  • 举报
回复
lz这个有没有解决了,我现在也遇到这样的问题。
k6693075 2012-04-24
  • 打赏
  • 举报
回复
- -没人回答么

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧