用C#在多张页打印大的图像(Using C# to print large images over

2019-06-25 15:54发布

我尝试写一些代码,将打印一个大的图像(1200宽×475高)在多个页面。

我试图分割图像在三个矩形(除以三的宽度),并调用e.Graphics.DrawImage三次,并且不工作。

如果我在一个页面,它的工作范围内确定大的形象,但我将如何去将图像分割成多个页面?

Answer 1:

关键是要获得图像的每一部分到自己的页面,那就是在做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");
    // 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(例如,如果你想显示预览后打印文档)。



文章来源: Using C# to print large images over mulitple pages