c# - 旋转图像并查看下一页

标签 c# .net image winforms picturebox

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

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

为什么旋转时看不到图像?

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

在下面的链接中,我提供了一个用于测试目的的测试 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();

        }
    }

enter image description here

最佳答案

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

关于c# - 旋转图像并查看下一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34185356/

相关文章:

c# - Java 的 ThreadPoolExecutor 等同于 C#?

c# - C# 随机数生成器线程安全吗?

c# - 如何使用emgu cv在图像中找到具有任意旋转角度的黑色方 block

ImageMagick - 将图像调整为正方形

image - 如何在opencv中的图像上覆盖文本而不修改基础矩阵

c# - 数据修改时自动从excel中读取数据

c# - 仅当实例出现一次时才匹配正则表达式

.net - 哪些技术适用于可通过 http 访问数据库的 Windows 数据库应用程序

c# - 当存在重叠匹配项时,在 Regex 中优先选择一个匹配项?

javascript - getElementById 具有动态创建的现有 id