Я пытаюсь повернуть изображение на основе тега EXIF. Я могу успешно обрабатывать поворот изображения, но миниатюра в проводнике Windows все еще перевернута. Изображение при открытии абсолютно нормальное. Проверена исправленная ориентация здесь. Проблема со следующим кодом заключается в том, что данные 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;
}