使用C#在多个页面上打印大图像

我正在尝试编写一些代码,可以在多个页面上打印大图像(1200宽x 475高)。

我尝试将图像划分为三个矩形(通过将宽度除以三)并调用e.Graphics.DrawImage三次,但这不起作用。

如果我在一个页面中指定大图像,它可以工作,但我如何将图像分割成多个页面?

诀窍是将图像的每个部分都放到自己的页面中,这是在PrintDocumentPrintPage事件中完成的。

我认为最简单的方法是将图像分成单独的图像,每页一个。 我将假设您已经可以处理(假设您尝试对图像进行分区;同样,只需将它们放在单独的图像上)。 然后我们创建PrintDocument实例,挂接PrintPage事件,然后转到:

 private List _pages = new List(); private int pageIndex = 0; private void PrintImage() { Image source = new Bitmap(@"C:\path\file.jpg"); // split the image into 3 separate images _pages.AddRange(SplitImage(source, 3)); PrintDocument printDocument = new PrintDocument(); printDocument.PrintPage += PrintDocument_PrintPage; PrintPreviewDialog previewDialog = new PrintPreviewDialog(); previewDialog.Document = printDocument; pageIndex = 0; previewDialog.ShowDialog(); // don't forget to detach the event handler when you are done printDocument.PrintPage -= PrintDocument_PrintPage; } private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e) { // Draw the image for the current page index e.Graphics.DrawImageUnscaled(_pages[pageIndex], e.PageBounds.X, e.PageBounds.Y); // increment page index pageIndex++; // indicate whether there are more pages or not e.HasMorePages = (pageIndex < _pages.Count); } 

请注意,在再次打印文档之前,您需要将pageIndex重置为0(例如,如果要在显示预览后打印文档)。