title: View large multi page Tif images within 100 milliseconds
body: <p>I'm using WinForms. Inside my form I have a <code>pictureBox</code> (set to <code>normal mode</code>), next and previous button. I want to resize and load Multipage TIF images quickly. When I go to the next page in the Multipage TIF image I experience a delay every time the image is drawn to the <code>pictureBox</code>. The average speed of the image takes about 800 milliseconds. <strong>I want the pages to load within 100 Milliseconds.</strong> </p>
  
  <p>I want the performance of <strong>processing large TIF images</strong> as fast as IrfanView. IrfanView is a small image viewing application. If you Download IrfanView you can see how fast the performance is. Currently I have another solution where I use multi-threading background worker to load the TIF pages into an array then I scale it down. This method requires some time initially, but the goal here is not having to wait.</p>
  
  <p>Is there a way to improve <code>Graphics.DrawImage</code> performance for large images in .NET?</p>
  
  <blockquote>
    <p>g.DrawImage(img, 0, 0, width, height); //This line causes the delay " 800 milliseconds depending on your computer"</p>
  </blockquote>
  
  <ul>
  <li>The size of TIF images i work with: Width=16800, Height=10800 </li>
  <li>Only Black and White Tif images</li>
  <li>Bit depth  = 1</li>
  <li>Resolution Unit = 2</li>
  </ul>
  
  <p><a href="https://i.stack.imgur.com/BILTh.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BILTh.gif" alt="enter image description here"></a></p>
  
  <pre><code> using System;
   using System.Collections.Generic;
   using System.ComponentModel;
   using System.Data;
   using System.Diagnostics;
   using System.Drawing;
   using System.Linq;
   using System.Text;
   using System.Threading.Tasks;
   using System.Windows.Forms;
  
  namespace Tif_Preformance_Question
  {
  public partial class Form1 : Form
  {
  
      int counter = -1;
      int frameCount = 0;
      Stopwatch s = new Stopwatch();
      Image img;
      Image[] images;
  
      public Form1()
      {
          InitializeComponent();
      }
  
      private void btn_Open_Click(object sender, EventArgs e)
      {
          var s = new Stopwatch();
          s.Start();
          s.Stop();
          this.Text = "Elapsed Time Milliseconds" + s.ElapsedMilliseconds;
  
  
          img = Image.FromFile(@"C:\image\Large_Tif_Image_15pages.tif");
          frameCount = img.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);
          images = new Image[frameCount];
  
          for (int i = 0; i &lt; frameCount; i++)
          {
              img.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, i);
              images[i] = (Image)img.Clone();
          }
          img.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, 0);
          pictureBox1.Image = (Image)img.Clone();
  
      }
  
      private void btn_Next_Click(object sender, EventArgs e)
      {
          counter++;
          if (counter &gt;= frameCount)
          {
              counter = frameCount - 1;
              btn_Next.Enabled = false;
          }
          btn_Next.Enabled = false;
          LoadPage();
          btn_Next.Enabled = true;
      }
  
      private void LoadPage()
      {
  
          StartWatch();
          img.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, counter);
          pictureBox1.Image = ResizeImage((Image)img.Clone(), pictureBox1.Width, pictureBox1.Height);
          pictureBox1.Refresh();
          Stopwatch();
      }
  
      public Image ResizeImage(Image img, int width, int height)
      {
          Bitmap b = new Bitmap(width, height);
          using (Graphics g = Graphics.FromImage((Image)b))
          {
              g.DrawImage(img, 0, 0, width, height);
          }
          return (Image)b;
      }
  
      private void StartWatch()
      {
          s.Start();
      }
      private void Stopwatch()
      {
  
          s.Stop();
          this.Text = "Elapsed Time Milliseconds: " + s.ElapsedMilliseconds;
          s.Reset();
      }
    }
  }
  </code></pre>
  
  <p><strong>References</strong> </p>
  
  <p><strong>IrfanView: </strong></p>
  
  <p><a href="http://www.irfanview.com/" rel="nofollow noreferrer">http://www.irfanview.com/</a></p>
  
  <p><strong>Test: Large TIF image Below</strong></p>
  
  <p><a href="http://www.filedropper.com/largetifimage15pages_2" rel="nofollow noreferrer">http://www.filedropper.com/largetifimage15pages_2</a></p>
  
  <p><strong>Visual Studio Solution</strong></p>
  
  <p><a href="http://www.filedropper.com/tifpreformancequestion_1" rel="nofollow noreferrer">http://www.filedropper.com/tifpreformancequestion_1</a></p>