我使用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
}
}
1条答案
按热度按时间polhcujo1#
这可能是在两个地方之一变慢了,要么加载巨大的文件(希望如此),要么选择活动帧。如果这是第一个问题,可能很容易修复,只需延迟加载图像一次: