小弟最近做一个项目,要用到图像的分页打印,首先要设置打印的页面,然后预览一下,最后选择其中的某一页进行打印。页面设置和预览以及分页功能都能实现,就是点击预览窗口的打印按钮时,打不出来啊。下面是我的代码,其实打印某一页只要能获得当前要打印的页码就行了是不是呢?如何实现啊? private static int totalPage = 0;//总页数
private static int currentPage = 0;//当前页
private static bool start_print = false;
private void MenuItem_print_Click(object sender, EventArgs e)
{
try
{
if (pictureBox1.Image == null)
{
MessageBox.Show("当前没有可打印的图像!");
return;
}
if (pageSetupDialog1.ShowDialog() == DialogResult.OK)//打印设置
{
totalPage = pictureBox1.Image.Height / pageSetupDialog1.PageSettings.Bounds.Height;//总页数
printDocument1.PrinterSettings.PrintRange = System.Drawing.Printing.PrintRange.SomePages;
printDocument1.PrinterSettings.ToPage = totalPage;
if (printPreviewDialog1.ShowDialog()==DialogResult.OK)//打印预览
{
//printDocument1.Print();
}
}
}
catch (Exception ee)
{
MessageBox.Show(ee.ToString());
}
} private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int fact_width = Convert.ToInt32(e.PageSettings.Bounds.Width);
int fact_height = Convert.ToInt32(e.PageSettings.Bounds.Height);
switch (currentPage)
{
case 999:
{
if (!start_print)
{
//
e.HasMorePages = false;
}
else
{
e.PageSettings.PrinterSettings.PrintRange = System.Drawing.Printing.PrintRange.Selection;
} break;
}
default:
{
Font font = new Font("宋体", 12, FontStyle.Regular);
float fontHeight = font.GetHeight(e.Graphics);
int font_height = (int)fontHeight;
int index = currentPage + 1;
string str = "轮胎规格:" + tyreSpecCode + " 图像编号:" + picname + " 打印时间:" + DateTime.Now.ToString() + " 总页数:" + totalPage + " 当前页:" + index;
e.Graphics.DrawString(str, font, Brushes.Black, new PointF(0, 0));
if (currentPage == 0)
{
Rectangle dest_rect = new Rectangle(0, font_height, fact_width, fact_height - font_height);
Rectangle src_rect = new Rectangle(0, 0, pictureBox1.Image.Width, fact_height - font_height);
e.Graphics.DrawImage(pictureBox1.Image, dest_rect, src_rect, GraphicsUnit.Pixel);
}
else
{
Rectangle dest_rect = new Rectangle(0, font_height, fact_width, fact_height - font_height);
Rectangle src_rect = new Rectangle(0, currentPage * (fact_height - font_height), pictureBox1.Image.Width, fact_height - font_height);
e.Graphics.DrawImage(pictureBox1.Image, dest_rect, src_rect, GraphicsUnit.Pixel);
}
if (index == 1)
{
e.PageSettings.PrinterSettings.FromPage = 1;
}
currentPage += 1;
e.HasMorePages = true;
if (totalPage - currentPage <= 0 && currentPage != 0)
{
e.PageSettings.PrinterSettings.ToPage = currentPage;
currentPage = 999;
e.HasMorePages = false;
start_print = true;
return;
}
break;
}
}
}
private static int currentPage = 0;//当前页
private static bool start_print = false;
private void MenuItem_print_Click(object sender, EventArgs e)
{
try
{
if (pictureBox1.Image == null)
{
MessageBox.Show("当前没有可打印的图像!");
return;
}
if (pageSetupDialog1.ShowDialog() == DialogResult.OK)//打印设置
{
totalPage = pictureBox1.Image.Height / pageSetupDialog1.PageSettings.Bounds.Height;//总页数
printDocument1.PrinterSettings.PrintRange = System.Drawing.Printing.PrintRange.SomePages;
printDocument1.PrinterSettings.ToPage = totalPage;
if (printPreviewDialog1.ShowDialog()==DialogResult.OK)//打印预览
{
//printDocument1.Print();
}
}
}
catch (Exception ee)
{
MessageBox.Show(ee.ToString());
}
} private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int fact_width = Convert.ToInt32(e.PageSettings.Bounds.Width);
int fact_height = Convert.ToInt32(e.PageSettings.Bounds.Height);
switch (currentPage)
{
case 999:
{
if (!start_print)
{
//
e.HasMorePages = false;
}
else
{
e.PageSettings.PrinterSettings.PrintRange = System.Drawing.Printing.PrintRange.Selection;
} break;
}
default:
{
Font font = new Font("宋体", 12, FontStyle.Regular);
float fontHeight = font.GetHeight(e.Graphics);
int font_height = (int)fontHeight;
int index = currentPage + 1;
string str = "轮胎规格:" + tyreSpecCode + " 图像编号:" + picname + " 打印时间:" + DateTime.Now.ToString() + " 总页数:" + totalPage + " 当前页:" + index;
e.Graphics.DrawString(str, font, Brushes.Black, new PointF(0, 0));
if (currentPage == 0)
{
Rectangle dest_rect = new Rectangle(0, font_height, fact_width, fact_height - font_height);
Rectangle src_rect = new Rectangle(0, 0, pictureBox1.Image.Width, fact_height - font_height);
e.Graphics.DrawImage(pictureBox1.Image, dest_rect, src_rect, GraphicsUnit.Pixel);
}
else
{
Rectangle dest_rect = new Rectangle(0, font_height, fact_width, fact_height - font_height);
Rectangle src_rect = new Rectangle(0, currentPage * (fact_height - font_height), pictureBox1.Image.Width, fact_height - font_height);
e.Graphics.DrawImage(pictureBox1.Image, dest_rect, src_rect, GraphicsUnit.Pixel);
}
if (index == 1)
{
e.PageSettings.PrinterSettings.FromPage = 1;
}
currentPage += 1;
e.HasMorePages = true;
if (totalPage - currentPage <= 0 && currentPage != 0)
{
e.PageSettings.PrinterSettings.ToPage = currentPage;
currentPage = 999;
e.HasMorePages = false;
start_print = true;
return;
}
break;
}
}
}
感谢您的回复。