移动到下一页时加快应用程序
Speed Up Application when Moving to the Next Page
我正在使用 WinForms。在我的表单中,我有一个显示图像文档的图片框。这些图像文件有很多页。我的表单上有一个打开、下一个和上一个按钮。在图片框中打开的文档中,下一个按钮前进一页,上一个按钮后退一页。我的表单上也有标签来指示有多少页,以及用户当前正在查看的页面。
我的代码存在问题,当我在我的图片框中打开大型图像文件时,例如有 1200 页的文档,然后单击下一步。页面加载缓慢。我想提高代码的性能。
如何才能更快地查看图像文档或改进我的代码?
我提供了一个 tif 文件来测试:http://www.filedropper.com/sampletifdocument5pages
private int int_Current_Page = 0;
private void btnNextImage_Click_1(object sender, EventArgs e)
{
Image image2;
try
{
if (int_Current_Page == 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
{ int_Current_Page = Convert.ToInt32(lblNumPages.Text); }
else
{
int_Current_Page++; //page increment
using (FileStream stream = new FileStream(@"C:\my_Image_document", FileMode.Open, FileAccess.Read))
{
image2 = Image.FromStream(stream);
Refresh_Image();
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnPrevImage_Click_1(object sender, EventArgs e)
{
if (int_Current_Page == 0) // it stops here if you reached the bottom, the first page of the tiff
{ int_Current_Page = 0; }
else
{
int_Current_Page--; // if its not the first page, then go to the previous page
Refresh_Image(); // refresh the image on the selected page
}
}
private void openButton_Click_1(object sender, EventArgs e)
{
Refresh_Image(); //Opens the large image document
}
private void Refresh_Image()
{
Image myImg; // setting the selected tiff
Image myBmp; // a new occurance of Image for viewing
using (FileStream stream = new FileStream(@"C:\my_Image_document", 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(int_Current_Page); // showing the number of page on which we're on
myImg.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, int_Current_Page); // going to the selected page
myBmp = new Bitmap(myImg, pictureBox1.Width, pictureBox1.Height);
pictureBox1.Image = myBmp; // showing the page in the pictureBox1
}
}
这可能在两个地方之一减慢了速度,加载巨大的文件(希望如此)或选择活动框架。如果这是第一个问题,只需延迟加载一次图像就可以轻松解决:
private int int_Current_Page = 0;
private void btnNextImage_Click_1(object sender, EventArgs e)
{
Image image2;
try
{
if (int_Current_Page == 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
{ int_Current_Page = Convert.ToInt32(lblNumPages.Text); }
else
{
int_Current_Page++; //page increment
Refresh_Image();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnPrevImage_Click_1(object sender, EventArgs e)
{
if (int_Current_Page == 0) // it stops here if you reached the bottom, the first page of the tiff
{ int_Current_Page = 0; }
else
{
int_Current_Page--; // if its not the first page, then go to the previous page
Refresh_Image(); // refresh the image on the selected page
}
}
private void openButton_Click_1(object sender, EventArgs e)
{
Refresh_Image(); //Opens the large image document
}
FileStream _stream;
Image _myImg; // setting the selected tiff
private void Refresh_Image()
{
// Image myImg; // setting the selected tiff - Now a member variable
Image myBmp; // a new occurance of Image for viewing
if (_myImg == null)
{
_stream = new FileStream(@"C:\my_Image_document", 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(int_Current_Page); // showing the number of page on which we're on
_myImg.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, int_Current_Page); // going to the selected page
myBmp = new Bitmap(_myImg, pictureBox1.Width, pictureBox1.Height);
pictureBox1.Image = myBmp; // showing the page in the pictureBox1
}
protected override void Dispose(bool disposing)
{
if (_stream != null) _stream.Dispose();
base.Dispose(disposing);
}
我正在使用 WinForms。在我的表单中,我有一个显示图像文档的图片框。这些图像文件有很多页。我的表单上有一个打开、下一个和上一个按钮。在图片框中打开的文档中,下一个按钮前进一页,上一个按钮后退一页。我的表单上也有标签来指示有多少页,以及用户当前正在查看的页面。
我的代码存在问题,当我在我的图片框中打开大型图像文件时,例如有 1200 页的文档,然后单击下一步。页面加载缓慢。我想提高代码的性能。
如何才能更快地查看图像文档或改进我的代码?
我提供了一个 tif 文件来测试:http://www.filedropper.com/sampletifdocument5pages
private int int_Current_Page = 0;
private void btnNextImage_Click_1(object sender, EventArgs e)
{
Image image2;
try
{
if (int_Current_Page == 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
{ int_Current_Page = Convert.ToInt32(lblNumPages.Text); }
else
{
int_Current_Page++; //page increment
using (FileStream stream = new FileStream(@"C:\my_Image_document", FileMode.Open, FileAccess.Read))
{
image2 = Image.FromStream(stream);
Refresh_Image();
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnPrevImage_Click_1(object sender, EventArgs e)
{
if (int_Current_Page == 0) // it stops here if you reached the bottom, the first page of the tiff
{ int_Current_Page = 0; }
else
{
int_Current_Page--; // if its not the first page, then go to the previous page
Refresh_Image(); // refresh the image on the selected page
}
}
private void openButton_Click_1(object sender, EventArgs e)
{
Refresh_Image(); //Opens the large image document
}
private void Refresh_Image()
{
Image myImg; // setting the selected tiff
Image myBmp; // a new occurance of Image for viewing
using (FileStream stream = new FileStream(@"C:\my_Image_document", 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(int_Current_Page); // showing the number of page on which we're on
myImg.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, int_Current_Page); // going to the selected page
myBmp = new Bitmap(myImg, pictureBox1.Width, pictureBox1.Height);
pictureBox1.Image = myBmp; // showing the page in the pictureBox1
}
}
这可能在两个地方之一减慢了速度,加载巨大的文件(希望如此)或选择活动框架。如果这是第一个问题,只需延迟加载一次图像就可以轻松解决:
private int int_Current_Page = 0;
private void btnNextImage_Click_1(object sender, EventArgs e)
{
Image image2;
try
{
if (int_Current_Page == 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
{ int_Current_Page = Convert.ToInt32(lblNumPages.Text); }
else
{
int_Current_Page++; //page increment
Refresh_Image();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnPrevImage_Click_1(object sender, EventArgs e)
{
if (int_Current_Page == 0) // it stops here if you reached the bottom, the first page of the tiff
{ int_Current_Page = 0; }
else
{
int_Current_Page--; // if its not the first page, then go to the previous page
Refresh_Image(); // refresh the image on the selected page
}
}
private void openButton_Click_1(object sender, EventArgs e)
{
Refresh_Image(); //Opens the large image document
}
FileStream _stream;
Image _myImg; // setting the selected tiff
private void Refresh_Image()
{
// Image myImg; // setting the selected tiff - Now a member variable
Image myBmp; // a new occurance of Image for viewing
if (_myImg == null)
{
_stream = new FileStream(@"C:\my_Image_document", 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(int_Current_Page); // showing the number of page on which we're on
_myImg.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, int_Current_Page); // going to the selected page
myBmp = new Bitmap(_myImg, pictureBox1.Width, pictureBox1.Height);
pictureBox1.Image = myBmp; // showing the page in the pictureBox1
}
protected override void Dispose(bool disposing)
{
if (_stream != null) _stream.Dispose();
base.Dispose(disposing);
}