提高浏览页面的性能

时间:2015-12-05 00:30:19

标签: c# .net image winforms performance

我正在使用WinForms。我的应用程序就像一个简单的图像文档(tif)查看器。我使用的tif / image文件有多个页面,所以按下一个按钮我的应用程序转到下一页。我的应用程序的问题是,当我移动到下一页时它很慢。以前,我的申请速度较慢。我已经对应用程序进行了一些修改以使其更快,但它仍然不够快。

我将应用程序的速度与Windows Photo Viewer进行了比较,结果是我的应用程序仍然需要改进性能。有谁知道如何让我的应用程序更快?

在下面的链接中,我提供了一个用于测试目的的示例文件。

链接:http://www.filedropper.com/tiftestingdoc

我的代码:

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

    private void Open_Btn_Click(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            lblFile.Text = openFileDialog1.FileName; //Shows the filename in the lable

            Image img = Image.FromFile(openFileDialog1.FileName);
            pictureBox1.Image = img;

            Size size = new Size(img.Height, img.Width);
            pictureBox1.Size = size;

            Open_Image_Control();

         }
    }

    public void Open_Image_Control()
    {
        Image myBmp;

        if (_myImg == null) //I made a copy of the file because i want to be able to modify the file in the directory for example go to directory and delete the file while still having the ability to view it on the application
        {
            _fileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
            File.Copy(@"C:\my_Image_document", _fileName);
            _stream = new FileStream(_fileName, FileMode.Open, FileAccess.Read);
            _myImg = Image.FromStream(_stream);
        }

        int intPages = _myImg.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page); // getting the number of pages of this tiff
        intPages--; // the first page is 0 so we must correct the number of pages to -1
        lblNumPages.Text = Convert.ToString(intPages); // showing the number of pages
        lblCurrPage.Text = Convert.ToString(intCurrPage); // showing the number of page on which we're on

        _myImg.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, intCurrPage); // going to the selected page


        myBmp = new Bitmap(_myImg, pictureBox1.Width, pictureBox1.Height);
            //myBmp = new Bitmap(_myImg, pictureBox1.Height, pictureBox1.Width);

        pictureBox1.Image = myBmp; // showing the page in the pictureBox1 
    }


    private void NextPage_btn_Click(object sender, EventArgs e)
    {
        if (intCurrPage == Convert.ToInt32(lblNumPages.Text)) // if you have reached the last page it ends here
                                                              // the "-1" should be there for normalizing the number of pages
        { intCurrPage = Convert.ToInt32(lblNumPages.Text); }
        else
        {

            intCurrPage++; //page increment (Goes to next page)
            Open_Image_Control();
        }
    }

enter image description here

1 个答案:

答案 0 :(得分:1)

将图像直接加载到PictureBox中,然后更改页面直接调用pictureBox1.Image.SelectActiveFrame()方法并刷新PictureBox。

这可以防止每次都为每个页面制作新的位图副本。 这导致每次分配额外的内存,并且在从页面复制所有像素时缓慢。

请参阅以下代码更改:

        // Variable to hold the current page number
    private int intCurrPage = 0; 
    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

            pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
            pictureBox1.Size = new Size(pictureBox1.Image.Height, pictureBox1.Image.Width);

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

    private void NextPage_btn_Click(object sender, EventArgs e)
    {
        // Check that the current page is not going past the max page
        if (intCurrPage < (intTotalPages-1))
        {
            //page increment (Go to next page)
            intCurrPage++;

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

            // Adjust the size of the picturebox control to the size of the current page.
            // not sure if this is necessary, but including it due to prior example
            pictureBox1.Size = new Size(pictureBox1.Image.Height, pictureBox1.Image.Width);

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

            lblCurrPage.Text = intCurrPage.ToString();
        }
    }

另外,你复制图像的代码有一个硬编码的目录名,这对我来说是错误的,所以我把它删除了。