旋转图像并查看下一页

Rotate Image and View Next Page

我正在为我的应用程序使用 WinForms。我正在构建一个图像查看器。我的应用程序打开图像文档 (.tif) 文件。该应用程序能够转到下一页。

问题是,每次我尝试旋转图像并单击下一步时,图像都会停留在同一页上,但页码会增加。

为什么轮播时看不到图片?

如何旋转图像并转到下一页?

在下面的 link 中,我提供了用于测试目的的测试 tif 文档: http://www.filedropper.com/sampletifdocument5pages

我的代码:

    FileStream _stream;
    Image _myImg; // setting the selected tiff
    string _fileName;
    private int intCurrPage = 0; // defining the current page
    private int intTotalPages = 0;


    private void Open_Btn_Click(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            lblFile.Text = openFileDialog1.FileName;

            // Before loading you should check the file type is an image

            if (_myImg == null)
            {
                _fileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
                File.Copy(@lblFile.Text, _fileName);
                _stream = new FileStream(_fileName, FileMode.Open, FileAccess.Read);
                pictureBox1.Image = Image.FromStream(_stream);
            }

            //pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
            pictureBox1.Size = new Size(750, 1100);

            // Reset the current page when loading a new image.
            intCurrPage = 1;
            intTotalPages = pictureBox1.Image.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);

            lblNumPages.Text = intTotalPages.ToString();
            lblCurrPage.Text = "1";


        }
     }

    private void NextPage_btn_Click(object sender, EventArgs e)
    {
        if (intCurrPage <= (intTotalPages - 1))
        {
            if(Radio_90_Rotate.Checked)
            {
                pictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipNone);
            }
            if(Radio_180_Rotate.Checked)
            {
                pictureBox1.Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
            }

            // Directly increment the active frame within the image already in the PictureBox
            pictureBox1.Image.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, intCurrPage);

            //page increment (Go to next page)
            intCurrPage++;


            // Refresh the PictureBox so that it will show the currently active frame
            pictureBox1.Refresh();

            lblCurrPage.Text = intCurrPage.ToString();

        }
    }

RotateFlip 函数将更改源图像并将其拼合到只有一页。这意味着每次您查看应用了旋转的新页面时,我们都需要制作副本。

在此解决方案中,我使用源图像并在未应用旋转时简单地更改页面。但是当设置了旋转时,每个页面都会制作一个图像副本,然后旋转仅应用于副本。

使用您的示例图像加载每个页面需要时间。所以我实现了一个简单的标签消息,让用户知道它正在工作。

此外,您可以考虑研究 类 预构建的 tiff 文件,例如:https://bitmiracle.github.io/libtiff.net/

private Image _Source = null;
private int _TotalPages = 0;
private int _CurrentPage = 0;

private void Frm_TiffViewer_Load(object sender, EventArgs e)
{
    lbl_WaitMessage.Visible = false;

    // These two options can be adjusted as needed and probably should be set in the form control properties directly:
    pictureBox1.Size = new Size(750, 1100);
    pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}

private void ShowProcessingImageLabel()
{
    lbl_WaitMessage.Visible = true;
    Application.DoEvents();
}

private void DisplayPage(int PageNumber, RotateFlipType Change)
{
    if (pictureBox1.Image != null && pictureBox1.Image != _Source)
    {
        // Release memory for old rotated image
        pictureBox1.Image.Dispose();
    }

    //  set the variable to null for easy GC cleanup
    pictureBox1.Image = null;

    _Source.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, PageNumber - 1);

    pictureBox1.Image = new Bitmap(_Source);

    pictureBox1.Image.RotateFlip(Change);

    pictureBox1.Refresh();
}

private void DisplayPage(int PageNumber)
{
    ShowProcessingImageLabel();

    this.lblCurrPage.Text = PageNumber.ToString();

    // You could adjust the PictureBox size here for each frame  OR adjust the image to fit the picturebox nicely.
    if (Radio_90_Rotate.Checked == true)
    {
        DisplayPage(PageNumber, RotateFlipType.Rotate90FlipNone);
        lbl_WaitMessage.Visible = false;
        return;
    }
    else if (Radio_180_Rotate.Checked == true)
    {
        DisplayPage(PageNumber, RotateFlipType.Rotate180FlipNone);
        lbl_WaitMessage.Visible = false;
        return;
    }

    if (pictureBox1.Image != _Source)
    {
        if (pictureBox1.Image != null)
        {
            // Release memory for old copy and set the variable to null for easy GC cleanup
            pictureBox1.Image.Dispose();
            pictureBox1.Image = null;
        }

        pictureBox1.Image = _Source;
    }

    pictureBox1.Image.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, PageNumber-1);

    pictureBox1.Refresh();

    lbl_WaitMessage.Visible = false;
}


private void Open_Btn_Click(object sender, EventArgs e)
{
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        // Before loading you should check the file type is an image

        this._Source = Image.FromFile(openFileDialog1.FileName);

        _TotalPages = _Source.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);
        _CurrentPage = 1;

        lblCurrPage.Text = "1";
        lblFile.Text = openFileDialog1.FileName;
        this.lblNumPages.Text = _TotalPages.ToString();

        DisplayPage(_CurrentPage);
    }
}

private void NextPage_btn_Click(object sender, EventArgs e)
{
    if (_CurrentPage < _TotalPages)
    {
        _CurrentPage++;
    }

    DisplayPage(_CurrentPage);
}

private void b_Previous_Click(object sender, EventArgs e)
{
    if (_CurrentPage > 1)
    {
        _CurrentPage--;
    }

    DisplayPage(_CurrentPage);
}

private void Radio_90_Rotate_CheckedChanged(object sender, EventArgs e)
{
    DisplayPage(_CurrentPage);
}

private void Radio_180_Rotate_CheckedChanged(object sender, EventArgs e)
{
    DisplayPage(_CurrentPage);
}

private void Radio_0_Default_CheckedChanged(object sender, EventArgs e)
{
    DisplayPage(_CurrentPage);
}

我创建了自定义模式 v3.1

 public Image _Image_v3_1_CustomMode(Image b1, float angle,float dx,float dy,float sx,float sy)

        {
            Bitmap bitmap = new Bitmap(b1.Width, b1.Height);

            using(Graphics ehack = Graphics.FromImage(bitmap))
            {
                ehack.RotateTransform(angle);
                ehack.TranslateTransform(dx, dy);
                ehack.ScaleTransform(sx, sy);
                ehack.DrawImage(b1, 0, 0);


                return bitmap;
            }


    }