图像旋转但缩略图不是

问题描述:

我想旋转基于EXIF标记的图像。我能够成功处理图像的旋转,但在Windows资源管理器中的缩略图仍然是颠倒的。打开图像时绝对没问题。已验证修正的方向here.以下代码的问题是,EXIF数据似乎没有任何有关缩略图方向的信息。我要的是:图像旋转但缩略图不是

  1. 如果没有可用的缩略图方向,旋转缩略图和缩略图定位更新图像的元数据。

  2. 如果没有可用的缩略图方向信息,请旋转缩略图并添加缩略图方向的图像元数据。

我使用的代码是:

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; 
} 
+0

我深深的怀疑是,缩略图只是不遵循EXIF标签,所以改变了EXIF元数据后,你必须重新生成缩略图。 – mcepl

是一个老问题,但经过一番研究,我发现,即使去除图像(0x0112)的方向和的方向后,缩略图(0x5029),当再次尝试生成缩略图时,仍保留相同的方向。所以我检查了一些JPG的缩略图以字节“嵌入”。所以删除字节(0x501B)后,我能够正确生成缩略图。

简单的代码所示:

var rotateImage = Image.FromStream(fileStream); 
    switch (degree) 
    { 
     case eRotateImagem.Degree_90: 
      rotateImage.RotateFlip(RotateFlipType.Rotate90FlipNone); 
      break; 
     case eRotateImagem.Degree_180: 
      rotateImage.RotateFlip(RotateFlipType.Rotate180FlipNone); 
      break; 
     case eRotateImagem.Degree_270: 
      rotateImage.RotateFlip(RotateFlipType.Rotate270FlipNone); 
      break; 
    } 

    int orientationId = 0x0112; //Image orientation 
    int thumbnailOrientationId = 0x5029; //Thumbnail orientation 
    int thumbnailBytes = 0x501B; //Thumbnail bytes 

    if (rotateImage.PropertyIdList.Contains(orientationId)) 
    { 
     rotateImage.RemovePropertyItem(orientationId); 
    } 
    if (rotateImage.PropertyIdList.Contains(thumbnailOrientationId)) 
    { 
     rotateImage.RemovePropertyItem(thumbnailOrientationId); 
    } 
    if (rotateImage.PropertyIdList.Contains(thumbnailBytes)) 
    { 
     rotateImage.RemovePropertyItem(thumbnailBytes); 
    }