簡體   English   中英

提升瀏覽頁面的性能

[英]Increase Performance Navigating Through Pages

我正在使用WinForms。 我的應用程序就像一個簡單的圖像文檔(tif)查看器。 我使用的tif /圖像文檔有多個頁面,因此,按下next按鈕,我的應用程序將轉到下一頁。 我的應用程序的問題在於,當我移至下一頁時,它的速度很慢。 以前,我的應用程序速度較慢。 我對應用程序進行了一些修改,以使其更快,但是仍然不夠快。

我已經將應用程序的速度與Windows Photo Viewer進行了比較,結果是我的應用程序仍需要提高性能。 有誰知道我可以如何使我的應用程序更快?

在下面的鏈接中,我提供了一個示例tif文檔用於測試。

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

在此處輸入圖片說明

將圖像直接加載到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();
        }
    }

另外,您用於復制圖像的代碼具有硬編碼的目錄名稱,這對我來說是錯誤的,因此我刪除了該名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM