title: Use libtiff in C# to convert from one TIFF format to another
body: <p>I have a TIFF using JPEG format the WPF / C# can not handle via <code>TiffBitmapDecoder</code>. Our clients use the file format and our current C++ and Java code handles it.</p>
  
  <p>I need to convert this to a format I can display using <code>TiffBitmapDecoder</code> or standard <code>BitmapImage</code>. It looks like the C# version of libtiff is the way to go but I am not having any luck converting in code.</p>
  
  <p>Here is my attempt - I always end up with corrupt files.</p>
  
  <pre><code>Boolean doSystemLoad = false;
  Tiff tiff = null;
  
  try
  {
      tiff = Tiff.Open(file, "r");
  }
  catch (Exception e) // TIFF could not handle, let OS do it
  {
      doSystemLoad = true;
  }
  if (tiff != null)
  {
      width = Double.Parse(tiff.GetField(TiffTag.IMAGEWIDTH)[0].Value.ToString());
      height = Double.Parse(tiff.GetField(TiffTag.IMAGELENGTH)[0].Value.ToString());
  
      int bits = Int32.Parse(tiff.GetField(TiffTag.BITSPERSAMPLE)[0].Value.ToString());
      int samples = Int32.Parse(tiff.GetField(TiffTag.SAMPLESPERPIXEL)[0].Value.ToString());
      string compression = tiff.GetField(TiffTag.COMPRESSION)[0].Value.ToString();
  
      Console.WriteLine("Image is " + width + " x " + height + "   bits " + bits + " sample " + samples);
      Console.WriteLine("Compression " + compression);
  
      // We allow OS to load anything that is not JPEG compression
      doSystemLoad = compression.ToLower().IndexOf("jpeg") == -1;
  
      string tempFile = Path.GetTempFileName() + ".tiff";
  
      // Convert here then load converted via OS
      if (!doSystemLoad)
      {
          Console.WriteLine("&gt;&gt; Attempting to convert... " + tempFile);
          Console.WriteLine("  Scan line  " + tiff.ScanlineSize());
  
          Tiff tiffOut = Tiff.Open(tempFile, "w");
          tiffOut.SetField(TiffTag.IMAGEWIDTH, width);
          tiffOut.SetField(TiffTag.IMAGELENGTH, height);
          tiffOut.SetField(TiffTag.BITSPERSAMPLE, bits);
          tiffOut.SetField(TiffTag.SAMPLESPERPIXEL, samples);
          tiffOut.SetField(TiffTag.ROWSPERSTRIP, 1L);
          tiffOut.SetField(TiffTag.COMPRESSION, Compression.NONE);
          tiffOut.SetField(TiffTag.ORIENTATION, BitMiracle.LibTiff.Classic.Orientation.TOPLEFT);
          tiffOut.SetField(TiffTag.FAXMODE, FaxMode.CLASSF);
          tiffOut.SetField(TiffTag.GROUP3OPTIONS, 5);
  
          tiffOut.SetField(TiffTag.PHOTOMETRIC, Photometric.RGB);
  
          tiffOut.SetField(TiffTag.FILLORDER, FillOrder.MSB2LSB);
          tiffOut.SetField(TiffTag.PLANARCONFIG, PlanarConfig.CONTIG);
          tiffOut.SetField(TiffTag.RESOLUTIONUNIT, ResUnit.INCH);
          tiffOut.SetField(TiffTag.XRESOLUTION, 100.0);
          tiffOut.SetField(TiffTag.YRESOLUTION, 100.0);
          tiffOut.SetField(TiffTag.SUBFILETYPE, FileType.PAGE);
          tiffOut.SetField(TiffTag.PAGENUMBER, new object[] { 1, 1 });
          tiffOut.SetField(TiffTag.PAGENAME, "Page 1");
  
          Byte[] scanLine = new Byte[tiff.ScanlineSize() + 5000];
          for (int row = 0; row &lt; height; row++)
          {
              tiff.ReadScanline(scanLine, row);
              tiffOut.WriteScanline(scanLine, row);
          }
          tiffOut.Dispose();
      }
  
      tiff.Dispose();
  
      Stream imageStreamSource = new FileStream(tempFile, FileMode.Open, FileAccess.Read, FileShare.Read);
      TiffBitmapDecoder decoder = new TiffBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
      BitmapSource bitmapSource = decoder.Frames[0];
  
      width = bitmapSource.Width;
      height = bitmapSource.Height;
  
      imageMain.Width = width;
      imageMain.Height = height;
      imageMain.Source = bitmapSource;
  }
  
  if (doSystemLoad)
  {
      Stream imageStreamSource = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);
      TiffBitmapDecoder decoder = new TiffBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
      BitmapSource bitmapSource = decoder.Frames[0];
  
      width = bitmapSource.Width;
      height = bitmapSource.Height;
  
      imageMain.Width = width;
      imageMain.Height = height;
      imageMain.Source = bitmapSource;
  }
  </code></pre>