我正在尝试从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是否存在问题,或者我做错了什么。
答案 0 :(得分:3)
这是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();
}