Unable to view multiple tiff files in .Net file

I am trying to view and print several TIFF files from a Windows C # 2005 application. Printing works fine, but when I send my PrintDocument to PrintPreviewDialog, I get two images of the first page, and not because of the first and second pages. I also have the same problem when I use PrintPreviewControl.

Below is the form code with 2 buttons, PrintDocument and PrintPreviewDialog, which demonstrate the problem.

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();
    }
}

Does anyone know if there is a problem with PrintPreviewDialog in the .NET Framework or am I something wrong.

+3
source share
1 answer

This is a bug with the function Graphics.DrawImage().

: Graphics.DrawImage

:

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();
}
+3

Source: https://habr.com/ru/post/1706409/


All Articles