【问题标题】:Image rotated but thumbnail is not图像旋转但缩略图未旋转
【发布时间】:2016-12-23 19:42:55
【问题描述】:

我正在尝试根据 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;
}

【问题讨论】:

  • 我深深怀疑缩略图只是不跟随EXIF标签,所以在更改EXIF元数据后,您必须重新生成缩略图。

标签: c# image-processing exif


【解决方案1】:

是一个老问题,但经过一些研究,我注意到即使在删除图像的方向(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);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-14
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    • 2015-10-01
    • 2011-05-12
    • 2014-12-18
    • 1970-01-01
    相关资源
    最近更新 更多