title: Image rotated but thumbnail is not
body: <p>I am trying to rotate the image based on EXIF tag. I am able to handle the rotation of image successfully but the thumbnail in windows explorer is still upside down. The image when opened is absolutely fine. Verified the corrected orientation <a href="http://regex.info/exif.cgi" rel="nofollow">here.</a> The problem with the following code is that the EXIF data does not seem to have any information about thumbnail orientation. What I want is:</p>
  
  <ol>
  <li><p>If there is thumbnail orientation available, rotate the thumbnail and update image's metadata for thumbnail orientation.</p></li>
  <li><p>If there is no thumbnail orientation information available, rotate the thumbnail and add image's metadata for thumbnail orientation.</p></li>
  </ol>
  
  <p>The code I am using is:</p>
  
  <pre><code>public static RotateFlipType RotateImageByExifOrientationData(Image img, string oldFileName, string sourceFilePath, out string newFileName)
  {    
      int orientationId = 0x0112;//Image orientation
      int thumbnailOrientationId = 0x5029;//Thumbnail orientation
      var fType = RotateFlipType.RotateNoneFlipNone;
  
      if (img.PropertyIdList.Contains(orientationId))
      {
          var pItem = img.GetPropertyItem(orientationId);
          //Get the orientation
          fType = GetRotateFlipTypeByExifOrientationData(pItem.Value[0]);
          if (fType != RotateFlipType.RotateNoneFlipNone)
          {
              img.RotateFlip(fType);
  
              // Read orientation tag. Update to normal so that the other clients(image viewer or browser) will not rotate the rotated image.                                      
              // Force value to 1
              pItem.Value = BitConverter.GetBytes((short)1);
              img.SetPropertyItem(pItem);
              PropertyItem thumbnailItem;
              if (img.PropertyIdList.Contains(thumbnailOrientationId))
              {
                  //If thumbnail metadata is available, update it.
                  thumbnailItem = img.GetPropertyItem(thumbnailOrientationId);
                  thumbnailItem.Value = BitConverter.GetBytes((short)1);
                  img.SetPropertyItem(thumbnailItem);
              }
              else
              {
                  //If thumbnail metadata is not available, add appropriate metadata.
                  thumbnailItem = img.PropertyItems[0];
                  thumbnailItem.Id = thumbnailOrientationId;
                  thumbnailItem.Type = 2;
                  thumbnailItem.Value = BitConverter.GetBytes((short)1);
                  thumbnailItem.Len = thumbnailItem.Value.Length;
                  img.SetPropertyItem(thumbnailItem);
              }
              newFileName = "Rotated_" + oldFileName;
              string targetFilePath = sourceFilePath + newFileName ;
              ImageFormat targetFormat = ImageFormat.Jpeg;
              img.Save(targetFilePath, targetFormat);
              File.Delete(sourceFilePath + oldFileName);//Delete old file.
          }
      }
      return fType;
  }
  </code></pre>