EXIF 태그를 기반으로 이미지를 회전하려고합니다. 성공적으로 이미지 회전을 처리 할 수 있지만 Windows 탐색기의 축소판 그림은 여전히 거꾸로되어 있습니다. 열 때 이미지는 절대적으로 좋습니다. 수정 된 방향 확인 here. 다음 코드의 문제점은 EXIF 데이터에 축소판 방향에 대한 정보가없는 것입니다. 내가 원하는 것은 : 가능한 썸네일 방향이이미지가 회전되었지만 썸네일이 아닙니다.
경우, 썸네일 썸네일 방향에 대한 업데이트 이미지의 메타 데이터를 회전 할 수 있습니다.
사용할 수있는 썸네일 방향 정보가없는 경우 썸네일을 회전하고 썸네일 방향에 대한 이미지의 메타 데이터를 추가하십시오.
내가 사용하고있는 코드는 다음과 같습니다
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;
}
내 깊은 의심 썸네일 그냥 EXIF 태그를 따르지 않는, 그래서는 EXIF 메타 데이터를 변경 한 후, 당신은 미리보기를 재생해야합니다 :
간단한 코드를 보여줍니다. – mcepl