我正在尝试从C#2005 Windows应用程序预览和打印多页TIFF文件。打印效果很好,但是当我将PrintDocument发送到PrintPreviewDialog时,得到的是第一页的两个图像,而不是第一页和第二页的图像。当我使用PrintPreviewControl时,我也有同样的问题。
下面是带有2个按钮的表单的代码,一个PrintDocument和一个PrintPreviewDialog演示了此问题。
public partial class Form1 : Form
{
private Image m_Image;
private Int32 m_CurrentPage;
private Int32 m_PageCount;
private void Form1_Load(object sender, EventArgs e)
{
m_Image = Image.FromFile(".\\Test-2-Page-Image.tif");
m_PageCount = m_Image.GetFrameCount(FrameDimension.Page);
}
private void printDocument_BeginPrint(object sender, PrintEventArgs e)
{
m_CurrentPage = 0;
m_PageCount = m_Image.GetFrameCount(FrameDimension.Page);
}
private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
m_Image.SelectActiveFrame(FrameDimension.Page, m_CurrentPage);
e.Graphics.DrawImage(m_Image, 0, 0);
++m_CurrentPage;
e.HasMorePages = (m_CurrentPage < m_PageCount);
}
private void btnPreview_Click(object sender, EventArgs e)
{
printPreviewDialog.Document = printDocument;
printPreviewDialog.ShowDialog();
}
private void btnPrint_Click(object sender, EventArgs e)
{
printDocument.Print();
}
}
有谁知道.Net框架中的PrintPreviewDialog是否存在问题,或者我做错了什么。
最佳答案
Graphics.DrawImage()
函数是一个错误。
该问题记录在这里:Graphics.DrawImage Bug
工作代码如下所示:
img.SelectActiveFrame(FrameDimension.Page, curPage);
using(MemoryStream stm = new MemoryStream())
{
img.Save(stm, imgCodecInfo, encParams); // save to memory stream
Bitmap bmp = (Bitmap)Image.FromStream(stm);
e.Graphics.DrawImage((Image)bmp,0,0);
bmp.Dispose();
}