2016-08-17 3 views
3

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

답변

0

은 오래된 질문이지만, 몇 가지 조사 후, 나는 눈치 심지어 이미지의 방향 (0x0112)과의 방향을 제거한 후 thumbnail (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); 
    } 
관련 문제