title: Get pages from multi tiff
body: <p>NET library a retrieve count of pages in multi tiff. I use sample from 
  <a href="http://bitmiracle.com/libtiff/help/retrieve-number-of-pages-in-tiff-image.aspx" rel="nofollow">http://bitmiracle.com/libtiff/help/retrieve-number-of-pages-in-tiff-image.aspx</a>.</p>
  
  <pre><code>    public int GetNumberOfPages(Tiff image)
      {
          int pageCount = 0;
          do
          {
              ++pageCount;
          } while (image.ReadDirectory());
  
          return pageCount;
      }
  
      [TestMethod]
      public void GetNumberOfPages()
      {
          using (Tiff tiffFromFile = Tiff.Open(@"test.tif", "r"))
          {
              int numberOfPages = GetNumberOfPages(tiffFromFile);
          }
  
      }
  </code></pre>
  
  <p>I get exception:  <strong>Cannot write to a closed TextWriter.</strong></p>
  
  <p>StackTrace:</p>
  
  <pre><code>at System.IO.__Error.WriterClosed()
     at System.IO.StringWriter.Write(String value)
     at Microsoft.VisualStudio.TestTools.TestTypes.Unit.ThreadSafeStringWriter.Write(String value)
     at System.IO.TextWriter.Write(String format, Object arg0)
     at System.IO.TextWriter.SyncTextWriter.Write(String format, Object arg0)
     at BitMiracle.LibTiff.Classic.TiffErrorHandler.WarningHandler(Tiff tif, String method, String format, Object[] args)
     at BitMiracle.LibTiff.Classic.Tiff.WarningExt(Tiff tif, Object clientData, String method, String format, Object[] args)
     at BitMiracle.LibTiff.Classic.Tiff.ReadDirectory()
  </code></pre>
  
  <p>The code is identical from sample. I can’t find root of this problem.</p>