且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-12 22:03:01

诀窍是将图像的每个部分都放入其中自己的页面,这是在 PrintPage PrintDocument 我认为最简单的方法是将图像分成单独的图像,每页一个。我会假设你已经可以处理它了(假设你尝试分割图像;同样的事情,只需将它们放在单独的图像上)。然后我们创建PrintDocument实例,连接PrintPage事件,然后转到:
  private List< Image> _pages = new List< Image>(); 
private int pageIndex = 0;

private void PrintImage()
{
Image source = new Bitmap(@C:\path\file.jpg);
//将图像分成3个独立的图像
_pages.AddRange(SplitImage(source,3));

PrintDocument printDocument = new PrintDocument();
printDocument.PrintPage + = PrintDocument_PrintPage;
PrintPreviewDialog previewDialog = new PrintPreviewDialog();
previewDialog.Document = printDocument;
pageIndex = 0;
previewDialog.ShowDialog();
//完成后不要忘记分离事件处理程序
printDocument.PrintPage - = PrintDocument_PrintPage;
}

private void PrintDocument_PrintPage(object sender,PrintPageEventArgs e)
{
//绘制当前页面索引
e.Graphics的图像。 DrawImageUnscaled(_pages [pageIndex],
e.PageBounds.X,
e.PageBounds.Y);
//增加页面索引
pageIndex ++;
//表示是否有更多页面
e.HasMorePages =(pageIndex< _pages.Count);

$ / code>

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


I am trying to write some code that will print a large image (1200 width x 475 height) over multiple pages.

I tried partitioning the image over three rectangles (by dividing the width by three) and calling e.Graphics.DrawImage three times and that's not working.

If I specify the large image within one page, it works, but how would I go about splitting the image into multiple pages?

The trick is to get each part of the image into its own page, and that is done in the PrintPage event of the PrintDocument.

I think that the easiest approach is to split the image up into separate images, one for each page. I will assume that you can handle that already (given you try with partitioning the image; same thing, just put them onto separate images). Then we create the PrintDocument instance, hook up the PrintPage event, and go:

private List<Image> _pages = new List<Image>();
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);   
}

Note that you will need to reset pageIndex to 0 before printing the document again (for instance, if you want to print the document after showing the preview).