我正在尝试从 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();
}
关于c# - 无法在 .Net 中预览多页 tiff 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/748735/