使用C#在多个页面上打印大图像 [英] Using C# to print large images over mulitple pages

查看:136
本文介绍了使用C#在多个页面上打印大图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一些能够在多个页面上打印大图像(1200宽x 475高)的代码。



我尝试将图像分成三个矩形(通过将宽度除以三)并调用e.Graphics.DrawImage三次,这是行不通的。



如果我在一个页面中指定大图像,但我该如何将图像分成多个页面?

解决方案

诀窍是将图像的每个部分都放入其中自己的页面,这是在 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).

这篇关于使用C#在多个页面上打印大图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆